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.MongoCollectionCount.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 queryData = getNamedParam(argStruct, "query", null);

    try {//from  w  w  w . j  a v a 2  s  .  c o  m
        Document qry = null;
        int count = 0;
        long start = System.currentTimeMillis();
        MongoCollection<Document> col = db.getCollection(collection);

        if (queryData != null) {
            qry = getDocument(queryData);
            count = (int) col.count(qry);
        } else
            count = (int) col.count();

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

        return new cfNumberData(count);

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

From source file:com.bluedragon.mongo.MongoCollectionDistinct.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 key = getNamedStringParam(argStruct, "key", null);
    if (key == null)
        throwException(_session, "please specify a key");

    cfData query = getNamedParam(argStruct, "query", null);

    try {//from  ww  w  .  ja v  a 2 s .  c  o  m

        DistinctIterable<String> result;
        if (query != null)
            result = db.getCollection(collection).distinct(key, String.class).filter(getDocument(query));
        else
            result = db.getCollection(collection).distinct(key, String.class);

        cfArrayData arr = cfArrayData.createArray(1);

        result.forEach(new Block<String>() {

            @Override
            public void apply(final String st) {
                try {
                    arr.addElement(new cfStringData(st));
                } catch (cfmRunTimeException e) {
                }
            }
        });

        return arr;

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

From source file:com.bluedragon.mongo.MongoCollectionDrop.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");

    try {/*w ww .ja v a 2s . co m*/
        db.getCollection(collection).drop();
        return cfBooleanData.TRUE;
    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.bluedragon.mongo.MongoCollectionFind.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");

    int size = getNamedIntParam(argStruct, "size", -1);
    int skip = getNamedIntParam(argStruct, "skip", -1);
    cfData sort = getNamedParam(argStruct, "sort", null);
    cfData fields = getNamedParam(argStruct, "fields", null);

    try {// w  w  w .  j  a va2  s .  c o  m
        MongoCollection<Document> col = db.getCollection(collection);

        // Get the initial cursor
        FindIterable<Document> cursor;
        long start = System.currentTimeMillis();
        Document qry = getDocument(query);

        cursor = col.find(qry);

        if (fields != null)
            cursor = cursor.projection(getDocument(fields));

        // Are we sorting?
        if (sort != null)
            cursor = cursor.sort(getDocument(sort));

        // Are we limiting
        if (skip != -1)
            cursor = cursor.skip(skip);

        // How many we bringing back
        if (size != -1)
            cursor = cursor.limit(size);

        // Now we can run the query
        cfArrayData results = cfArrayData.createArray(1);

        cursor.forEach(new Block<Document>() {

            @SuppressWarnings("rawtypes")
            @Override
            public void apply(final Document st) {
                try {
                    results.addElement(tagUtils.convertToCfData((Map) st));
                } catch (cfmRunTimeException e) {
                }
            }
        });

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

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

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

License:Open Source License

@SuppressWarnings("rawtypes")
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 update = getNamedParam(argStruct, "update", null);
    if (update == null)
        throwException(_session, "please specify update");

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

    try {/*from w  w  w  . j  av  a2 s  . c  om*/

        MongoCollection<Document> col = db.getCollection(collection);
        FindOneAndUpdateOptions findOneAndUpdateOptions = new FindOneAndUpdateOptions();

        if (getNamedParam(argStruct, "fields", null) != null)
            findOneAndUpdateOptions.projection(getDocument(getNamedParam(argStruct, "fields", null)));

        if (getNamedParam(argStruct, "sort", null) != null)
            findOneAndUpdateOptions.sort(getDocument(getNamedParam(argStruct, "sort", null)));

        findOneAndUpdateOptions.upsert(getNamedBooleanParam(argStruct, "upsert", false));

        if (getNamedBooleanParam(argStruct, "returnnew", false))
            findOneAndUpdateOptions.returnDocument(ReturnDocument.AFTER);

        Document qry = getDocument(query);
        long start = System.currentTimeMillis();

        Document result = col.findOneAndUpdate(qry, getDocument(update), findOneAndUpdateOptions);

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

        return tagUtils.convertToCfData((Map) result);

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

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

License:Open Source License

@SuppressWarnings("rawtypes")
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 find");

    cfData fields = getNamedParam(argStruct, "fields", null);

    try {//from   w ww  .  j  a v a  2 s  .c  o  m
        MongoCollection<Document> col = db.getCollection(collection);

        long start = System.currentTimeMillis();
        Document qry = getDocument(query);

        FindIterable<Document> cursor = col.find(qry).limit(1);

        if (fields != null)
            cursor = cursor.projection(getDocument(fields));

        _session.getDebugRecorder().execMongo(col, "findone", qry, System.currentTimeMillis() - start);
        return tagUtils.convertToCfData((Map) cursor.first());

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

From source file:com.bluedragon.mongo.MongoCollectionIndexDrop.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 index = getNamedStringParam(argStruct, "index", null);

    try {/*w w w .  jav a  2s .c o m*/
        if (index != null)
            db.getCollection(collection).dropIndex(index);
        else
            db.getCollection(collection).dropIndexes();

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

From source file:com.bluedragon.mongo.MongoCollectionIndexEnsure.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' parameter");

    cfData keys = getNamedParam(argStruct, "keys", null);
    if (keys == null)
        throwException(_session, "please specify 'keys' parameter");

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

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

        db.getCollection(collection).createIndex(getDocument(keys),
                new IndexOptions().background(true).unique(getNamedBooleanParam(argStruct, "unique", false)));

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

From source file:com.bluedragon.mongo.MongoCollectionInsert.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 insert");

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

        MongoCollection<Document> col = db.getCollection(collection);

        if (data.getDataType() == cfData.CFARRAYDATA) {

            cfArrayData idArr = cfArrayData.createArray(1);
            List<Document> list = new ArrayList<Document>();
            cfArrayData arrdata = (cfArrayData) data;

            for (int x = 0; x < arrdata.size(); x++) {
                Document doc = getDocument(arrdata.getData(x + 1));
                idArr.addElement(new cfStringData(String.valueOf(doc.get("_id"))));
                list.add(doc);
            }

            long start = System.currentTimeMillis();
            col.insertMany(list);
            _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start);

            return idArr;

        } else {

            Document doc = getDocument(data);

            long start = System.currentTimeMillis();
            col.insertOne(doc);
            _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start);

            return new cfStringData(String.valueOf(doc.get("_id")));
        }

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

From source file:com.bluedragon.mongo.MongoCollectionMapReduce.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 map = getNamedStringParam(argStruct, "map", null);
    if (map == null)
        throwException(_session, "please specify a map");

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

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

    String action = getNamedStringParam(argStruct, "type", "replace").toLowerCase();
    String finalize = getNamedStringParam(argStruct, "finalize", null);
    cfData query = getNamedParam(argStruct, "query", null);

    try {/*  w ww .j av a 2s  . c om*/
        MapReduceIterable<Document> mi = db.getCollection(collection).mapReduce(map, reduce);

        if (query != null)
            mi.filter(getDocument(query));

        if (finalize != null)
            mi.finalizeFunction(finalize);

        mi.collectionName(outputcollection);
        mi.action(MapReduceAction.valueOf(action));

        // Kick start the map reduce
        mi.first();

        return cfBooleanData.TRUE;

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