Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:com.bluedragon.mongo.MongoCollectionRemove.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query to remove");

    try {/*  w  w  w . j a  v  a 2s  . c  om*/

        MongoCollection<Document> col = db.getCollection(collection);
        col.deleteMany(getDocument(query));
        return cfBooleanData.TRUE;

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.bluedragon.mongo.MongoCollectionRename.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    String name = getNamedStringParam(argStruct, "name", null);
    if (name == null)
        throwException(_session, "please specify a name");

    try {/*w w w . j a v  a2  s .  c o  m*/

        db.getCollection(collection).renameCollection(new MongoNamespace(db.getName(), name),
                new RenameCollectionOptions().dropTarget(getNamedBooleanParam(argStruct, "droptarget", false)));

        return cfBooleanData.TRUE;

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.bluedragon.mongo.MongoCollectionSave.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData data = getNamedParam(argStruct, "data", null);
    if (data == null)
        throwException(_session, "please specify data to save");

    try {//  ww  w .  j  av a 2  s  .c  o  m
        Document doc = getDocument(data);
        MongoCollection<Document> col = db.getCollection(collection);
        long start = System.currentTimeMillis();

        if (doc.containsKey("_id")) {
            col.updateOne(new Document("_id", doc.get("_id")), new Document("$set", doc));
        } else {
            col.insertOne(doc);
        }

        _session.getDebugRecorder().execMongo(col, "save", doc, System.currentTimeMillis() - start);

        return cfBooleanData.TRUE;

    } catch (Exception me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.bluedragon.mongo.MongoCollectionUpdate.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData data = getNamedParam(argStruct, "data", null);
    if (data == null)
        throwException(_session, "please specify data to update");

    cfData query = getNamedParam(argStruct, "query", null);
    if (query == null)
        throwException(_session, "please specify query to update");

    boolean upsert = getNamedBooleanParam(argStruct, "upsert", false);
    boolean multi = getNamedBooleanParam(argStruct, "multi", false);

    try {// w w w.  j av  a  2 s  . c o  m

        MongoCollection<Document> col = db.getCollection(collection);
        Document qry = getDocument(query);
        Document dat = getDocument(data);
        long start = System.currentTimeMillis();

        if (multi)
            col.updateMany(qry, dat, new UpdateOptions().upsert(upsert));
        else
            col.updateOne(qry, dat, new UpdateOptions().upsert(upsert));

        _session.getDebugRecorder().execMongo(col, "update", qry, System.currentTimeMillis() - start);

        return cfBooleanData.TRUE;

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.chadcover.Homework_23.java

License:Apache License

public static void main(String[] args) {
    MongoClient client = new MongoClient();
    MongoDatabase database = client.getDatabase("students");
    MongoCollection<Document> collection = database.getCollection("gradesBak");

    long count = collection.count();
    System.out.println(count);/*from   w  w w  .ja  va  2s  .co  m*/

    Bson filter = eq("type", "homework");
    Bson sort = ascending("student_id", "score");
    Bson projection = fields(include("student_id", "score"));

    // better for large datasets
    MongoCursor<Document> result = collection.find(filter).sort(sort).projection(projection).iterator();
    try {
        int lastId = 0;
        int counter = 0;
        while (result.hasNext()) {
            Document cur = result.next();
            int id = (Integer) cur.get("student_id");
            // when moving to new student
            // delete that record, which is the lowest homework score
            if (id != lastId || counter == 0) {
                double grade = (Double) cur.get("score");
                System.out.println("Delete this score: " + grade);
                collection.deleteOne(eq("_id", cur.get("_id")));
            } else {
                // do nothing
            }
            lastId = id;
            counter++;
        }
    } finally {
        result.close();
    }

}

From source file:com.cognifide.aet.vs.metadata.MetadataDAOMongoDBImpl.java

License:Apache License

private MongoCollection<Document> getMetadataCollection(DBKey dbKey) throws StorageException {
    final String dbName = MongoDBClient.getDbName(dbKey.getCompany(), dbKey.getProject());
    final MongoDatabase database = client.getDatabase(dbName, true);
    if (database == null) {
        throw new StorageException(
                String.format("Database %s does not exist! Contact AET administrators.", dbName));
    }/*from   w  w  w.  j  ava2s.  c  om*/
    return database.getCollection(METADATA_COLLECTION_NAME);
}

From source file:com.cognitive.cds.invocation.mongo.IntentMappingDao.java

License:Apache License

public DeleteResult deleteIntent(String intentName) throws JsonProcessingException {
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("intent");
    MongoCollection<Document> collection = db.getCollection("cdsintent");

    BasicDBObject query = new BasicDBObject();
    query.append("name", intentName);

    DeleteResult result = collection.deleteOne(query);
    return result;
}

From source file:com.cognitive.cds.invocation.mongo.IntentMappingDao.java

License:Apache License

public Document updateIntentMapping(IntentMapping im) throws JsonProcessingException {
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("intent");
    MongoCollection<Document> collection = db.getCollection("cdsintent");

    Document filter = new Document();
    if (im.get_id() != null) {
        filter.put("_id", im.get_id());
    } else if (im.getId() != null && !im.getId().isEmpty()) {
        filter.put("_id", new ObjectId(im.getId()));
    } else {/*from   w  ww  .ja  v a 2 s .  co  m*/
        return null;
    }
    Document obj = collection.find(filter).first();
    Document result = null;

    if (obj != null) {
        try {
            String objectJson = mapper.writeValueAsString(im);
            Document doc = Document.parse(objectJson);
            doc.put("_id", im.get_id());

            result = collection.findOneAndReplace(filter, doc);
            if (cache.containsKey(im.getName())) {
                cache.put(im.getName(), im);
            }

        } catch (IOException e) {
            logger.error("========> Deserialize: " + e.toString());
        }
    }
    return result;
}

From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java

License:Apache License

public DeleteResult deleteWorkProduct(String id) throws JsonProcessingException {

    mongoDbDao.setDatabase("work");
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("work");
    MongoCollection<Document> collection = db.getCollection("work");

    BasicDBObject query = new BasicDBObject();
    query.append("_id", new ObjectId(id));

    DeleteResult result = collection.deleteOne(query);
    return result;
}

From source file:com.cognitive.cds.invocation.mongo.WorkProductDao.java

License:Apache License

public UpdateResult updateWorkProduct(WorkProduct wp) throws JsonProcessingException {
    mongoDbDao.setDatabase("work");
    MongoClient mongo = mongoDbDao.getMongoClient();
    MongoDatabase db = mongo.getDatabase("work");
    MongoCollection<Document> collection = db.getCollection("work");

    Document filter = new Document();
    if (wp.getId() == null)
        return null;
    else/*from  w ww .j a  v  a  2s .c  om*/
        filter.put("_id", new ObjectId(wp.getId()));

    Document obj = collection.find(filter).first();
    UpdateResult result = null;

    if (obj != null) {
        try {
            String objectJson = JsonUtils.getMapper().writeValueAsString(wp);
            Document doc = Document.parse(objectJson);
            result = collection.updateOne(filter, new Document("$set", new Document("workproduct", doc)));

        } catch (IOException e) {
            logger.error("========> Deserialize: " + e.toString());
        }
    }
    return result;
}