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.iotivity.cloud.rdserver.MongoDB.java

License:Open Source License

/**
 * API for finding resources matched filterValue of filterKey and a
 * particular device ID in collection//from   w ww .j  ava2  s  . com
 *
 * @param di
 *            device id
 * @param filterKey
 *            field name in collection
 * @param filterValue
 *            field value about field name
 * @param tablename
 *            collection name
 * @return ArrayList<PublishPayloadFormat> - array list of resource
 *         information
 */
public ArrayList<PublishPayloadFormat> readResourceAboutDid(String di, String filterKey, String filterValue,
        String tablename) {
    MongoCollection<Document> collection = db.getCollection(tablename);
    ArrayList<PublishPayloadFormat> resourceFormatList = new ArrayList<PublishPayloadFormat>();
    MongoCursor<Document> cursor = collection
            .find(Filters.and(Filters.eq(Constants.RS_DEVICE_ID, di), Filters.eq(filterKey, filterValue)))
            .iterator();
    try {
        while (cursor.hasNext()) {
            Document doc = cursor.next();
            resourceFormatList.add(convertDocumentToResourceFormat(doc));
        }
    } finally {
        cursor.close();
    }

    return resourceFormatList;
}

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

License:Open Source License

@Override
public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    Document retrieved = collection.find(new Document("_id", primaryKey)).first();
    return serializer.parse(JSON.serialize(retrieved), entityClass);
}

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

License:Open Source License

@Override
public <T extends JaqpotEntity> T find(Class<T> entityClass, Object primaryKey, List<String> fields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    Document retrieved = collection.find(new Document("_id", primaryKey)).projection(include(fields)).first();
    return serializer.parse(JSON.serialize(retrieved), entityClass);
}

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

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max) {
    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 w  w  . jav  a 2  s.  c  o  m
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).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> findSorted(Class<T> entityClass, Map<String, Object> properties,
        Integer start, Integer max, List<String> ascendingFields, 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 -> {/*from  w ww .  j av a  2  s  .co  m*/
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties))
            .sort(Sorts.orderBy(Sorts.ascending(ascendingFields), 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.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,
        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 -> {//  w  w  w .  j av a 2  s  . c o m
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).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,
        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 -> {//from  ww w .  j av  a2  s .c  om
        Map<String, Object> all = new HashMap<>();
        all.put("$all", e.getValue());
        properties.put(e.getKey(), all);
    });

    List<T> result = new ArrayList<>();
    collection.find(new Document(properties)).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.jaqpot.core.db.entitymanager.MongoDBEntityManager.java

License:Open Source License

@Override
public <T extends JaqpotEntity> List<T> find(Class<T> entityClass, List<String> keys, List<String> fields) {
    MongoDatabase db = mongoClient.getDatabase(database);
    MongoCollection<Document> collection = db.getCollection(collectionNames.get(entityClass));
    List<T> result = new ArrayList<>();
    Document query = new Document("_id", new Document("$in", keys));
    Document filter = new Document();
    fields.stream().forEach(f -> filter.put(f, 1));
    collection.find(query).projection(filter)
            .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> find(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, Integer start, Integer max) {
    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 ww w .  j  a  v a 2 s. 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).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> findSorted(Class<T> entityClass, Map<String, Object> properties,
        List<String> fields, Integer start, Integer max, List<String> ascendingFields,
        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 -> {//from w w w .j  a  v  a  2s .  com
        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.orderBy(Sorts.ascending(ascendingFields), 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;
}