Example usage for com.mongodb Block Block

List of usage examples for com.mongodb Block Block

Introduction

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

Prototype

Block

Source Link

Usage

From source file:com.averageloser.mongodemo.Model.BookDBHelper.java

@Override
public Book getDocument(MongoCollection<Document> collection, Bson object) {
    Book book = new Book();

    Document doc = (Document) object;

    FindIterable<Document> iterable = collection.find(doc);
    iterable.forEach(new Block<Document>() {
        @Override/*from   w w w  .  j  av  a  2  s. c  o m*/
        public void apply(Document t) {
            book.setTitle(t.getString("title"));
            book.setDescription(t.getString("description"));
            book.setAuthor(t.getString("author"));
            book.setPublisher(t.getString("publisher"));
        }

    });

    return book;
}

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

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

    if (data.getDataType() == cfData.CFARRAYDATA) {
        cfArrayData arrData = (cfArrayData) data;

        if (arrData.size() == 0)
            throwException(_session, "please specify at least one pipeline");

        for (int x = 0; x < arrData.size(); x++)
            pipelineList.add(getDocument(arrData.getData(x + 1)));

    } else {/* w  w w  .  ja v  a  2 s  . c o  m*/
        pipelineList.add(getDocument(data));
    }

    try {
        MongoCollection<Document> col = db.getCollection(collection);
        long start = System.currentTimeMillis();

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

        col.aggregate(pipelineList).forEach(new Block<Document>() {

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

        });

        _session.getDebugRecorder().execMongo(col, "aggregate", null, System.currentTimeMillis() - start);

        return results;
    } catch (Exception e) {
        throwException(_session, e.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 w ww .  ja va 2  s .  c  om

        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.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 {/*  www.  j a  v a2  s  .  com*/
        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.MongoCollectionList.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoClient client = getMongoClient(_session, argStruct);

    try {/*from w  ww  .  j  a  v  a  2s .  com*/
        cfArrayData arr = cfArrayData.createArray(1);

        client.listDatabaseNames().forEach(new Block<String>() {

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

        });

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

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

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoClient client = getMongoClient(_session, argStruct);

    try {//from w  ww.ja v a 2 s  .com
        cfArrayData arr = cfArrayData.createArray(1);

        client.listDatabaseNames().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.DA.assignment1.Query.Demonstrate.java

public static void selectMovie(MongoCollection<Document> rating) throws IOException {
    System.out.println("Please enter userID: you can check the rating records ");
    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    String userID = br1.readLine();

    FindIterable<Document> movies = rating.find(new Document("userID", userID));

    movies.forEach(new Block<Document>() {
        @Override/*from   ww w  .j a v  a2  s .  c om*/
        public void apply(final Document document) {
            System.out.println(document);
        }
    });

}

From source file:com.DA.assignment1.Query.Demonstrate.java

public static void Tag(MongoCollection<Document> tag) throws IOException {
    System.out.println("Please enter movieID:");

    BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
    String movieID = br1.readLine();

    FindIterable<Document> tags = tag.find(new Document("movieID", movieID));
    tags.forEach(new Block<Document>() {
        @Override/* www.  j a v  a 2  s  .  com*/
        public void apply(final Document document) {
            System.out.println(document);
        }
    });
}

From source file:com.DA.assignment1.Query.Question1.java

public void Answer() {

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");

    FindIterable<Document> iterable1 = db.getCollection("movies").find(new Document("title", "Copycat (1995)"));
    iterable1.forEach(new Block<Document>() {
        @Override//  www  .j  a  v a 2s  . com
        public void apply(final Document document) {
            System.out.println(document);
            //System.out.println("hah");

        }
    });

}

From source file:com.DA.assignment1.Query.Question2.java

public void Answer() {

    MongoClient mongoClient = new MongoClient();
    MongoDatabase db = mongoClient.getDatabase("test");

    AggregateIterable<Document> iterable = db.getCollection("movies")
            .aggregate(asList(new Document("$unwind", "$generes"),
                    new Document("$group",
                            new Document("_id", "$generes").append("count", new Document("$sum", 1))),
                    new Document("$sort", new Document("count", -1))));

    iterable.forEach(new Block<Document>() {
        @Override// w w w  .jav  a  2 s  .  c  o  m
        public void apply(final Document document) {
            System.out.println(document.toJson());
        }
    });

}