Example usage for com.mongodb DBCursor getKeysWanted

List of usage examples for com.mongodb DBCursor getKeysWanted

Introduction

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

Prototype

@Nullable
public DBObject getKeysWanted() 

Source Link

Document

Gets the fields to be returned.

Usage

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

public void find(final ButtonBase button) {
    final DBCollection col = getCollectionNode().getCollection();
    final DBObject query = ((DocBuilderField) getBoundUnit(Item.findQuery)).getDBObject();
    final DBObject fields = ((DocBuilderField) getBoundUnit(Item.findFields)).getDBObject();
    final DBObject sort = ((DocBuilderField) getBoundUnit(Item.findSort)).getDBObject();
    final DBObject hint = ((DocBuilderField) getBoundUnit(Item.findHint)).getDBObject();
    final int skip = getIntFieldValue(Item.findSkip);
    final int limit = getIntFieldValue(Item.findLimit);
    final int batchSize = getIntFieldValue(Item.findBatchSize);
    final boolean explain = getBooleanFieldValue(Item.findExplain);
    final boolean export = getBooleanFieldValue(Item.findExport);

    if (export) {
        exportToFile(col, query, fields, sort, skip, limit, batchSize);
    } else {/*w  w w. j a  v a  2 s  . c  om*/
        new DbJob() {
            @Override
            public Object doRun() {
                // this does not actually block, may not need dbjob
                DBCursor cur = col.find(query, fields, skip, batchSize);
                if (sort != null) {
                    cur.sort(sort);
                }
                if (limit > 0) {
                    cur.limit(limit);
                }
                if (hint != null) {
                    cur.hint(hint);
                }
                if (explain) {
                    return cur.explain();
                }

                // force cursor to start
                cur.hasNext();
                return cur;
            }

            @Override
            public String getNS() {
                return col.getFullName();
            }

            @Override
            public String getShortName() {
                return "Find";
            }

            @Override
            public DBObject getRoot(Object result) {
                if (result == null || !(result instanceof DBCursor)) {
                    return null;
                }
                DBCursor res = (DBCursor) result;
                BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
                obj.put("server", res.getServerAddress().toString());
                obj.put("query", res.getQuery());
                obj.put("fields", res.getKeysWanted());
                obj.put("options", res.getOptions());
                obj.put("readPreference", res.getReadPreference().toDBObject());
                obj.put("numSeen", res.numSeen());
                obj.put("numGetMores", res.numGetMores());
                // want skip, limit, batchsize
                return obj;
            }

            @Override
            public ButtonBase getButton() {
                return button;
            }
        }.addJob();

    }
}

From source file:com.edgytech.umongo.CollectionPanel.java

License:Apache License

static void doFind(final DBCollection col, final DBObject query, final DBObject fields, final DBObject sort,
        final int skip, final int limit, final int batchSize, final boolean explain, final DBObject hint,
        final int options) {
    new DbJob() {
        @Override//from ww w. j  av  a  2  s  . com
        public Object doRun() {
            // this does not actually block, may not need dbjob
            DBCursor cur = col.find(query, fields).skip(skip).batchSize(batchSize).addOption(options);
            if (sort != null) {
                cur.sort(sort);
            }
            if (limit > 0) {
                cur.limit(limit);
            }
            if (hint != null) {
                cur.hint(hint);
            }
            if (explain) {
                return cur.explain();
            }

            // force cursor to start
            cur.hasNext();
            return cur;
        }

        @Override
        public String getNS() {
            return col.getFullName();
        }

        @Override
        public String getShortName() {
            return "Find";
        }

        @Override
        public DBObject getRoot(Object result) {
            if (result == null || !(result instanceof DBCursor)) {
                return null;
            }
            DBCursor res = (DBCursor) result;
            BasicDBObject obj = new BasicDBObject("cursorId", res.getCursorId());
            obj.put("query", res.getQuery());
            obj.put("fields", res.getKeysWanted());
            obj.put("options", res.getOptions());
            obj.put("readPreference", res.getReadPreference().toDBObject());
            obj.put("numSeen", res.numSeen());
            obj.put("numGetMores", res.numGetMores());
            // want skip, limit, batchsize
            return obj;
        }
    }.addJob();
}

From source file:com.effektif.mongo.LoggingCursor.java

License:Apache License

public LoggingCursor(MongoCollection mongoCollection, DBCursor cursor) {
    super(cursor.getCollection(), cursor.getQuery(), cursor.getKeysWanted(), cursor.getReadPreference());
    this.mongoCollection = mongoCollection;
    this.cursor = cursor;
}