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(@Nullable final DBObject query, @Nullable final DBObject projection,
        @Nullable final DBObject sort, final ReadPreference readPreference) 

Source Link

Document

Get a single document from collection.

Usage

From source file:org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore.java

License:Apache License

@CheckForNull
protected <T extends Document> T findUncached(Collection<T> collection, String key,
        DocumentReadPreference docReadPref) {
    log("findUncached", key, docReadPref);
    DBCollection dbCollection = getDBCollection(collection);
    final Stopwatch watch = startWatch();
    boolean isSlaveOk = false;
    boolean docFound = true;
    try {/*from  w  w w  . j  a va  2s .c om*/
        ReadPreference readPreference = getMongoReadPreference(collection, null, key, docReadPref);

        if (readPreference.isSlaveOk()) {
            LOG.trace("Routing call to secondary for fetching [{}]", key);
            isSlaveOk = true;
        }

        DBObject obj = dbCollection.findOne(getByKeyQuery(key).get(), null, null, readPreference);

        if (obj == null) {
            docFound = false;
            return null;
        }
        T doc = convertFromDBObject(collection, obj);
        if (doc != null) {
            doc.seal();
        }
        return doc;
    } finally {
        stats.doneFindUncached(watch.elapsed(TimeUnit.NANOSECONDS), collection, key, docFound, isSlaveOk);
    }
}