Example usage for com.mongodb DBCollection find

List of usage examples for com.mongodb DBCollection find

Introduction

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

Prototype

public DBCursor find(final DBObject query) 

Source Link

Document

Select documents in collection and get a cursor to the selected documents.

Usage

From source file:com.images3.data.impl.MongoDBAccess.java

License:Apache License

protected PageCursor selectPageCursorById(String id) {
    DBCollection coll = getDatabase().getCollection("PageCursor");
    BasicDBObject criteria = new BasicDBObject().append("id", id);
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }// w w  w.ja v  a 2 s . co m
    return getObjectMapper().mapToPageCursor((BasicDBObject) cursor.next());
}

From source file:com.images3.data.impl.MongoDBAccess.java

License:Apache License

private PageCursor selectPageCursorByPreviousId(String id) {
    DBCollection coll = getDatabase().getCollection("PageCursor");
    BasicDBObject criteria = new BasicDBObject().append("previousPageCursorId", id);
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }//  w  w  w.  j  av a  2  s  . co m
    return getObjectMapper().mapToPageCursor((BasicDBObject) cursor.next());
}

From source file:com.images3.data.impl.TemplateAccessImplMongoDB.java

License:Apache License

public boolean isDuplicatedTemplateName(String imagePlantId, String name) {
    DBCollection coll = getDatabase().getCollection("Template");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId).append("nameKey",
            name.toLowerCase());//from ww w.  j  av  a 2 s  . c  om
    DBCursor cursor = coll.find(criteria);
    return cursor.hasNext();
}

From source file:com.images3.data.impl.TemplateAccessImplMongoDB.java

License:Apache License

public TemplateOS selectTemplateById(TemplateIdentity id) {
    DBCollection coll = getDatabase().getCollection("Template");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", id.getImagePlantId()).append("nameKey",
            id.getTemplateName().toLowerCase());
    DBCursor cursor = coll.find(criteria);
    if (!cursor.hasNext()) {
        return null;
    }/*from   www  . j a  va  2  s.co m*/
    return getObjectMapper().mapToTemplateOS((BasicDBObject) cursor.next());
}

From source file:com.images3.data.impl.TemplateAccessImplMongoDB.java

License:Apache License

private List<TemplateOS> getTemplatesByImagePlantId(String imagePlantId, Boolean isArchived, Page pageCursor) {
    DBCollection coll = getDatabase().getCollection("Template");
    int skipRecords = (pageCursor.getStart() - 1) * pageCursor.getSize();
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId);
    if (null != isArchived) {
        criteria.append("isArchived", isArchived);
    }/* ww w.  j  av  a  2 s.c  o m*/
    List<DBObject> objects = coll.find(criteria).skip(skipRecords).limit(pageCursor.getSize()).toArray();
    List<TemplateOS> templates = new ArrayList<TemplateOS>(objects.size());
    for (DBObject obj : objects) {
        templates.add(getObjectMapper().mapToTemplateOS((BasicDBObject) obj));
    }
    return templates;
}

From source file:com.images3.data.impl.TemplateAccessImplMongoDB.java

License:Apache License

private List<TemplateOS> getTemplatesByImagePlantId(String imagePlantId, Boolean isArchived) {
    DBCollection coll = getDatabase().getCollection("Template");
    BasicDBObject criteria = new BasicDBObject().append("imagePlantId", imagePlantId);
    if (null != isArchived) {
        criteria.append("isArchived", isArchived);
    }/*from  w w  w. j  a v  a  2  s.com*/
    List<DBObject> objects = coll.find(criteria).toArray();
    List<TemplateOS> templates = new ArrayList<TemplateOS>(objects.size());
    for (DBObject obj : objects) {
        templates.add(getObjectMapper().mapToTemplateOS((BasicDBObject) obj));
    }
    return templates;
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public <E> List<E> getColumnsById(String schemaName, String joinTableName, String joinColumnName,
        String inverseJoinColumnName, Object parentId, Class columnJavaType) {
    List<E> foreignKeys = new ArrayList<E>();

    DBCollection dbCollection = mongoDb.getCollection(joinTableName);
    BasicDBObject query = new BasicDBObject();

    query.put(joinColumnName, MongoDBUtils.populateValue(parentId, parentId.getClass()));
    KunderaCoreUtils.printQuery("Find by Id:" + query, showQuery);
    DBCursor cursor = dbCollection.find(query);
    DBObject fetchedDocument = null;/*from w w  w.  ja  v  a 2 s .com*/

    while (cursor.hasNext()) {
        fetchedDocument = cursor.next();
        Object foreignKey = fetchedDocument.get(inverseJoinColumnName);
        foreignKey = MongoDBUtils.getTranslatedObject(foreignKey, foreignKey.getClass(), columnJavaType);
        foreignKeys.add((E) foreignKey);
    }
    return foreignKeys;
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public Object[] findIdsByColumn(String schemaName, String tableName, String pKeyName, String columnName,
        Object columnValue, Class entityClazz) {
    EntityMetadata metadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz);

    List<Object> primaryKeys = new ArrayList<Object>();

    DBCollection dbCollection = mongoDb.getCollection(tableName);
    BasicDBObject query = new BasicDBObject();

    query.put(columnName, MongoDBUtils.populateValue(columnValue, columnValue.getClass()));

    DBCursor cursor = dbCollection.find(query);
    KunderaCoreUtils.printQuery("Find id by column:" + query, showQuery);
    DBObject fetchedDocument = null;/*from ww  w  .  jav a 2s.  com*/

    while (cursor.hasNext()) {
        fetchedDocument = cursor.next();
        Object primaryKey = fetchedDocument.get(pKeyName);
        primaryKey = MongoDBUtils.getTranslatedObject(primaryKey, primaryKey.getClass(),
                metadata.getIdAttribute().getJavaType());
        primaryKeys.add(primaryKey);
    }

    if (primaryKeys != null && !primaryKeys.isEmpty()) {
        return primaryKeys.toArray(new Object[0]);
    }
    return null;

}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

@Override
public <E> List<E> findAll(Class<E> entityClass, String[] columnsToSelect, Object... keys) {
    EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClass);

    log.debug("Fetching data from " + entityMetadata.getTableName() + " for Keys " + keys);

    DBCollection dbCollection = mongoDb.getCollection(entityMetadata.getTableName());

    BasicDBObject query = new BasicDBObject();

    query.put("_id", new BasicDBObject("$in", keys));

    DBCursor cursor = dbCollection.find(query);
    KunderaCoreUtils.printQuery("Find collection:" + query, showQuery);
    List entities = new ArrayList<E>();
    while (cursor.hasNext()) {
        DBObject fetchedDocument = cursor.next();

        populateEntity(entityMetadata, entities, fetchedDocument);
    }/* w w  w  . j a v a 2  s  . co  m*/
    return entities;
}

From source file:com.impetus.client.mongodb.MongoDBClient.java

License:Apache License

/**
 * Method to find entity for given association name and association value.
 * /*from   w w w.j a  v a  2 s  .  co m*/
 * @param colName
 *            the col name
 * @param colValue
 *            the col value
 * @param entityClazz
 *            the entity clazz
 * @return the list
 */
public List<Object> findByRelation(String colName, Object colValue, Class entityClazz) {
    EntityMetadata m = KunderaMetadataManager.getEntityMetadata(kunderaMetadata, entityClazz);
    // you got column name and column value.
    DBCollection dbCollection = mongoDb.getCollection(m.getTableName());

    BasicDBObject query = new BasicDBObject();

    query.put(colName, MongoDBUtils.populateValue(colValue, colValue.getClass()));
    KunderaCoreUtils.printQuery("Find by relation:" + query, showQuery);
    DBCursor cursor = dbCollection.find(query);
    DBObject fetchedDocument = null;
    List<Object> results = new ArrayList<Object>();
    while (cursor.hasNext()) {
        fetchedDocument = cursor.next();
        populateEntity(m, results, fetchedDocument);
    }

    return results.isEmpty() ? null : results;
}