Example usage for com.mongodb DBCollection findOne

List of usage examples for com.mongodb DBCollection findOne

Introduction

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

Prototype

@Nullable
public DBObject findOne(final DBObject query, final DBObject projection, final ReadPreference readPreference) 

Source Link

Document

Get a single document from collection.

Usage

From source file:com.groupon.jenkins.mongo.MongoRepository.java

License:Open Source License

protected DBObject findOne(BasicDBObject query, DBObject fields, DBObject orderBy,
        ReadPreference readPreference) {
    MongoClient client = getClient();//  w  w  w.j  a v  a 2s . c  o m
    try {
        DBCollection collection = getCollection(client);
        return readPreference == null ? collection.findOne(query, fields, orderBy)
                : collection.findOne(query, fields, orderBy, readPreference);
    } finally {
        client.close();
    }
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public Map findOne(QueryInfo queryInfo) {
    DB db = mongoClient.getDB(queryInfo.dbName);
    DBCollection coll = db.getCollection(queryInfo.collName);
    DBObject record = null;//from  w  w  w  .  ja v  a2s. com
    //if (queryInfo.query != null) {
    record = coll.findOne(queryInfo.query, null, queryInfo.order);
    //} else {
    //    record = coll.findOne();
    //}
    if (record == null) {
        return null;
    }
    return record.toMap();
}

From source file:org.fastmongo.odm.bson.repository.BsonMongoTemplate.java

License:Apache License

public <T> T findOne(Class<? extends T> clazz, Query query, Query fields, Query orderBy) {
    DBCollection collection = getCollection(clazz);
    DBObject db = collection.findOne(query.toDbObject(), fields != null ? fields.toDbObject() : null,
            orderBy != null ? orderBy.toDbObject() : null);
    if (db == null) {
        return null;
    }/* ww w .j av a  2 s.  co  m*/

    return read(clazz, db);
}

From source file:org.fastmongo.odm.repository.MongoTemplate.java

License:Apache License

@Override
public DBObject findOne(String collectionName, Query query, Projection fields, Query orderBy) {
    DBCollection collection = getCollection(collectionName);

    DBObject dbQuery = query.toDbObject();
    DBObject dbFields = fields != null ? fields.toDbObject() : null;
    DBObject dbOrderBy = orderBy != null ? orderBy.toDbObject() : null;

    return collection.findOne(dbQuery, dbFields, dbOrderBy);
}

From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java

License:LGPL

/**
 * Returns a {@link DBObject} representing the entity which embeds the specified association.
 *//*from   www.ja va 2s .c o m*/
private DBObject getEmbeddingEntity(AssociationKey key, AssociationContext associationContext) {
    DBObject embeddingEntityDocument = associationContext.getEntityTuple() != null
            ? ((MongoDBTupleSnapshot) associationContext.getEntityTuple().getSnapshot()).getDbObject()
            : null;

    if (embeddingEntityDocument != null) {
        return embeddingEntityDocument;
    } else {
        ReadPreference readPreference = getReadPreference(associationContext);

        DBCollection collection = getCollection(key.getEntityKey());
        DBObject searchObject = prepareIdObject(key.getEntityKey());
        DBObject projection = getProjection(key, true);

        return collection.findOne(searchObject, projection, readPreference);
    }
}

From source file:org.hibernate.ogm.datastore.mongodb.MongoDBDialect.java

License:LGPL

private DBObject getObject(EntityKey key, TupleContext tupleContext) {
    ReadPreference readPreference = getReadPreference(tupleContext);

    DBCollection collection = getCollection(key);
    DBObject searchObject = prepareIdObject(key);
    BasicDBObject projection = getProjection(tupleContext);

    return collection.findOne(searchObject, projection, readPreference);
}