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(@Nullable final DBObject query, final DBCollectionFindOptions options) 

Source Link

Document

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

Usage

From source file:com.zjy.mongo.input.MongoInputSplit.java

License:Apache License

public DBCursor getCursor() {
    if (this.cursor == null) {
        DBCollection coll;
        if (this.authURI != null) {
            coll = MongoConfigUtil.getCollectionWithAuth(this.inputURI, this.authURI);
        } else {/*from   w w  w  . j  a v  a 2  s. c  o  m*/
            coll = MongoConfigUtil.getCollection(this.inputURI);
        }

        this.cursor = coll.find(this.query, this.fields).sort(this.sort);
        if (this.notimeout) {
            this.cursor.setOptions(Bytes.QUERYOPTION_NOTIMEOUT);
        }
        if (this.min != null) {
            this.cursor.addSpecial("$min", this.min);
        }
        if (this.max != null) {
            this.cursor.addSpecial("$max", this.max);
        }
    }
    return this.cursor;
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVMaintenance.java

License:Open Source License

private long[] doFindDanglingCommits(long expiry, TimeUnit unit) {
    long maxTime = getMaxTime(expiry, unit);

    //load all commits which are older than the expiry time. mark them as dangling
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    DBCursor commits = collCommits.find(
            new BasicDBObject(MongoDBConstants.TIMESTAMP,
                    new BasicDBObject("$not", new BasicDBObject("$gte", maxTime))), //also include commits without a timestamp
            new BasicDBObject(MongoDBConstants.ID, 1));
    IdSet danglingCommits = new IdHashSet();
    for (DBObject o : commits) {
        long cid = (Long) o.get(MongoDBConstants.ID);
        danglingCommits.add(cid);//from   www . j av a 2s.co m
    }

    //walk through all branches and eliminate commits which are not dangling
    DBCollection collBranches = _db.getDB().getCollection(MongoDBConstants.COLLECTION_BRANCHES);
    DBCursor branches = collBranches.find(new BasicDBObject(), new BasicDBObject(MongoDBConstants.CID, 1));
    VHistory history = _db.getHistory();
    IdSet alreadyCheckedCommits = new IdHashSet();
    for (DBObject o : branches) {
        long cid = (Long) o.get(MongoDBConstants.CID);
        while (cid != 0) {
            if (alreadyCheckedCommits.contains(cid)) {
                break;
            }
            alreadyCheckedCommits.add(cid);
            danglingCommits.remove(cid);
            cid = history.getParent(cid);
        }
    }

    //all remaining commits must be dangling
    return danglingCommits.toArray();
}

From source file:de.fhg.igd.mongomvcc.impl.MongoDBVMaintenance.java

License:Open Source License

private long[] doFindUnreferencedDocuments(String collection, long expiry, TimeUnit unit) {
    long maxTime = getMaxTime(expiry, unit);

    //fetch the OIDs of all documents older than the expiry time
    DBCollection collDocs = _db.getDB().getCollection(collection);
    DBCursor docs = collDocs.find(
            new BasicDBObject(MongoDBConstants.TIMESTAMP,
                    new BasicDBObject("$not", new BasicDBObject("$gte", maxTime))), //also include docs without a timestamp
            new BasicDBObject(MongoDBConstants.ID, 1));
    IdSet oids = new IdHashSet(docs.count());
    for (DBObject o : docs) {
        oids.add((Long) o.get(MongoDBConstants.ID));
    }/*from   www.ja  v  a  2s  .c o m*/

    //iterate through all commits and eliminate referenced documents
    DBCollection collCommits = _db.getDB().getCollection(MongoDBConstants.COLLECTION_COMMITS);
    for (DBObject o : collCommits.find()) {
        Commit c = Tree.deserializeCommit(o);
        Map<String, IdMap> allObjs = c.getObjects();
        IdMap objs = allObjs.get(collection);
        if (objs != null) {
            //eliminate OIDs referenced by this commit
            IdMapIterator mi = objs.iterator();
            while (mi.hasNext()) {
                mi.advance();
                oids.remove(mi.value());
            }
        }
    }

    //the remaining OIDs must be the unreferenced ones
    return oids.toArray();
}