Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

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

Prototype

FindIterable<TDocument> find(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> findSortedAsc(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, Integer start, Integer max, List<String> ascendingFields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {/*from   w  ww .j a v a2s.c o m*/
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).projection(filter).sort(Sorts.ascending(ascendingFields))
            .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> findSortedDesc(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, Integer start, Integer max, List<String> descendingFields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    properties.entrySet().stream().filter(e -> {
        return e.getValue() instanceof List;
    }).forEach(e -> {// w  w w .  j a  v a  2 s  . c  om
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).projection(filter).sort(Sorts.descending(descendingFields))
            .skip(start != null ? start : 0).limit(max != null ? max : DEFAULT_PAGE_SIZE)
            .map(document -> serializer.parse(JSON.serialize(document), entityClass)).into(result);
    return result;
}

From source file:org.jboss.as.test.compat.nosql.mongodb.jaxrs.ClientResource.java

License:Open Source License

@GET
@Produces({ "text/plain" })
public String get() {

    MongoCollection collection = null;
    Document query = null;//from  w ww.j a v  a  2 s  .  c  o  m
    try {
        collection = database.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.modules.StatefulTestBean.java

License:Open Source License

public String addUserComment() {
    MongoCollection collection = null;
    Document query = null;/*w ww.j  ava  2 s. c o  m*/
    try {
        // add a comment from user Melanie
        String who = "Melanie";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "123 Main Street").append("city", "Fastville")
                                .append("state", "MA").append("zip", 18180))
                .append("comment", "MongoDB Is Web Scale");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        // look up the comment from Melanie
        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.modules.StatefulTestBean.java

License:Open Source License

public String addProduct() {
    MongoCollection collection = null;
    Document query = null;//www.jav a 2 s.  c o m
    try {
        collection = injectedDatabase.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.StatefulTestBean.java

License:Open Source License

public String addUserComment() {
    MongoCollection collection = null;
    Document query = null;/*from   w  w  w  .  j ava  2  s.c  om*/
    try {
        // add a comment from user Melanie
        String who = "Melanie";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "123 Main Street").append("city", "Fastville")
                                .append("state", "MA").append("zip", 18180))
                .append("comment",
                        "I really love your new website but I have a lot of questions about using NoSQL versus a traditional RDBMS.  "
                                + "I would like to sign up for your 'MongoDB Is Web Scale' training session.");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        // look up the comment from Melanie
        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jboss.as.test.compat.nosql.mongodb.StatefulTestBean.java

License:Open Source License

public String addProduct() {
    MongoCollection collection = null;
    Document query = null;/* w  ww.  ja  v  a 2 s.  c o m*/
    try {
        collection = database.getCollection("company");
        String companyName = "Acme products";
        JsonObject object = Json.createObjectBuilder().add("companyName", companyName)
                .add("street", "999 Flow Lane").add("city", "Indiville").add("_id", companyName).build();
        Document document = Document.parse(object.toString());
        collection.insertOne(document);
        query = new Document("_id", companyName);
        FindIterable cursor = collection.find(query);
        Object dbObject = cursor.first();
        return dbObject.toString();
    } finally {
        if (query != null) {
            collection.drop();
        }
    }
}

From source file:org.jboss.as.test.compat.nosql.multiple.StatefulTestBean.java

License:Open Source License

public String addSalesComment() {
    MongoCollection collection = null;
    Document query = null;// w ww  .  ja va 2  s. co m
    try {
        // add a comment from Sales department
        String who = "InsideSalesTeam";
        Document comment = new Document("_id", who).append("name", who)
                .append("address",
                        new BasicDBObject("street", "inside sales").append("city", "internal")
                                .append("state", "internal").append("zip", 99999))
                .append("comment", "Need to sell sell sell");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);

        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}

From source file:org.jnosql.diana.mongodb.document.MongoDBDocumentCollectionManager.java

License:Open Source License

@Override
public List<DocumentEntity> select(DocumentQuery query) {
    String collectionName = query.getCollection();
    MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    Bson mongoDBQuery = DocumentQueryConversor.convert(
            query.getCondition().orElseThrow(() -> new IllegalArgumentException("condition is required")));
    return stream(collection.find(mongoDBQuery).spliterator(), false).map(this::of)
            .map(ds -> DocumentEntity.of(collectionName, ds)).collect(toList());

}

From source file:org.lambda3.indra.mongo.MongoIndraTranslator.java

License:Open Source License

private Map<String, List<String>> doTranslate(Set<String> tokens) {
    MongoCollection<Document> lexColl = getLexCollection();
    FindIterable<Document> lexs = lexColl.find(Filters.in(TERM_FIELD, tokens));

    Map<String, List<String>> res = new HashMap<>();
    for (Document doc : lexs) {
        Document tr = (Document) doc.get(TRANSLATION_FIELD);

        if (tr != null) {
            tr.remove(NULL_VALUE);/*from w  w  w.  j ava 2s . c o m*/
            res.put(doc.getString(TERM_FIELD), getRelevantTranslations((Map) tr));
        }
    }

    return res;
}