Example usage for com.mongodb.async SingleResultCallback SingleResultCallback

List of usage examples for com.mongodb.async SingleResultCallback SingleResultCallback

Introduction

In this page you can find the example usage for com.mongodb.async SingleResultCallback SingleResultCallback.

Prototype

SingleResultCallback

Source Link

Usage

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

/**
 *
 * @param collectionName//  w w  w . j  av  a2s.c o m
 * @return
 */
public boolean collectionExists(final String collectionName) {

    MongoIterable colls = mongoDatabase().listCollectionNames();

    List<String> collectionNames = new ArrayList<String>();

    try {
        final CountDownLatch countDownLatch = new CountDownLatch(1);//??

        colls.into(collectionNames, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                // logger.debug("?");
                countDownLatch.countDown();
            }
        });
        countDownLatch.await(2, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    System.out.println(collectionNames);
    System.out.println(collectionNames.size());

    for (String name : collectionNames) {
        if (name.equalsIgnoreCase(collectionName)) {
            return true;
        }
    }
    return false;
}

From source file:com.supermy.im.mongo.MongoRepository.java

License:Apache License

@Bean(name = "mongoCollection")
public MongoCollection mongoCollection() {
    System.out.println("*******************" + mycoll);

    final MongoDatabase mydb = mongoDatabase();

    /**/*  ww w.j  a  v  a 2 s .c  o m*/
     * ??,?;?;
     * 1???,?,????;
     */
    if (!collectionExists(mycoll)) {
        final CountDownLatch countDownLatch = new CountDownLatch(1);//??

        mydb.createCollection(mycoll, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                // logger.debug("?");

                MongoCollection coll = mydb.getCollection(mycoll);

                coll.createIndex(new Document("position", "2d"), new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                //?
                Document expire = Document.parse("{time:1},{expireAfterSeconds:10*60}");
                coll.createIndex(expire, new SingleResultCallback<Void>() {
                    @Override
                    public void onResult(final Void result, final Throwable t) {
                        // logger.debug("??");
                    }
                });

                countDownLatch.countDown();
            }
        });
        try {
            countDownLatch.await(2, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    MongoCollection coll = mydb.getCollection(mycoll);
    // TODO: 16/5/10  ;?
    //> db.location.ensureIndex( {position: "2d"} )

    return coll;
}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void createDocument(final String collectionName, Object object, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    try {/*from   www .j a va  2  s. c  om*/
        String json = mapper.writeValueAsString(object);

        Document doc = Document.parse(json);

        collection.insertOne(doc, new SingleResultCallback<Void>() {
            @Override
            public void onResult(final Void result, final Throwable t) {
                onInsertResultAction(t, "success", callback);
            }
        });

    } catch (JsonProcessingException e) {
        e.printStackTrace();
        callback.onError(e.getMessage());
    }

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void removeDocument(final String collectionName, Criteria criteria, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.deleteOne(criteria.getRestrictions(), new SingleResultCallback<DeleteResult>() {
        @Override/*www  .  j av a 2  s .c om*/
        public void onResult(final DeleteResult result, final Throwable t) {
            onDeleteResultAction(result, t, "success", callback);
        }
    });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void removeDocumentField(final String collectionName, Criteria criteria, String field,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), unset(field), new SingleResultCallback<UpdateResult>() {
        @Override/*from   w  w w  .java2s  .c  om*/
        public void onResult(final UpdateResult result, final Throwable t) {
            onUpdateResultAction(result, t,
                    result.getModifiedCount() + " " + collectionName + " entity successfully updated",
                    callback);
        }
    });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void updateDocumentField(final String collectionName, Criteria criteria, final String field,
        Object value, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), set(field, value),
            new SingleResultCallback<UpdateResult>() {
                @Override/*from   w ww  . j  av  a 2  s  . c o  m*/
                public void onResult(final UpdateResult result, final Throwable t) {
                    onUpdateResultAction(result, t,
                            "successfully updated EntityObject field= " + field + " Entity = " + collectionName,
                            callback);
                }
            });
}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void updateDocumentFields(final String collectionName, Criteria criteria, HashMap<String, String> map,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    List<Bson> list = new ArrayList<>();

    for (final Map.Entry m : map.entrySet()) {
        try {/* w  ww.  java2  s  .c o m*/
            Document doc = Document.parse(m.getValue().toString());
            list.add(set(m.getKey().toString(), doc));
        } catch (Exception e) {
            list.add(set(m.getKey().toString(), m.getValue().toString()));
        }
    }

    logger.info("updating EntityObject fields");
    collection.updateOne(criteria.getRestrictions(), combine(list), new SingleResultCallback<UpdateResult>() {
        @Override
        public void onResult(final UpdateResult result, final Throwable t) {
            onUpdateResultAction(result, t,
                    "successfully updated EntityObject fields Entity = " + collectionName, callback);
        }
    });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void pushIntoDocumentField(final String collectionName, Criteria criteria, final String field,
        List<String> values, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    List<Document> docs = new ArrayList<>();
    List<String> strings = new ArrayList<>();

    for (String value : values) {
        try {/*  w w  w  .  jav a 2s.  com*/
            docs.add(Document.parse(value));
        } catch (Exception e) {
            strings.add(value);
        }
    }

    if (!docs.isEmpty()) {

        collection.updateOne(criteria.getRestrictions(), addEachToSet(field, docs),
                new SingleResultCallback<UpdateResult>() {
                    @Override
                    public void onResult(final UpdateResult result, final Throwable t) {
                        onUpdateResultAction(result, t, "success", callback);
                    }
                });

    }

    if (!strings.isEmpty()) {

        collection.updateOne(criteria.getRestrictions(), addEachToSet(field, strings),
                new SingleResultCallback<UpdateResult>() {
                    @Override
                    public void onResult(final UpdateResult result, final Throwable t) {
                        onUpdateResultAction(result, t, "success", callback);
                    }
                });

    }

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void pullStringsFromDocumentField(final String collectionName, Criteria criteria, final String field,
        List<String> values, final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.updateOne(criteria.getRestrictions(), pullAll(field, values),
            new SingleResultCallback<UpdateResult>() {
                @Override//  w  ww.ja  v a 2  s  .  c  om
                public void onResult(final UpdateResult result, final Throwable t) {
                    onUpdateResultAction(result, t, "success", callback);
                }
            });

}

From source file:mongodb.clients.percunia.mongo.AsyncClient.java

License:Apache License

@Override
public void getDocumentFieldValueViaFilter(final String collectionName, Criteria criteria,
        final ResultCallback callback) {

    MongoCollection<Document> collection = database.getCollection(collectionName);

    collection.find(criteria.getRestrictions()).projection(criteria.getProjections())
            .first(new SingleResultCallback<Document>() {
                @Override//from ww w. ja va 2  s . co  m
                public void onResult(final Document document, final Throwable t) {
                    onDocumentResult(document, t, "success", callback);
                }
            });

}