/** * Sets a document describing the fields to return for all matching documents. * * @param projection the project document, which may be null. * @return this * @mongodb.driver.manual reference/method/db.collection.find/ Projection */ FindOptions projection(final DBObject projection) { options.projection(projection); return this; }
/** * Get a single document from collection by '_id'. * * @param id value of '_id' field of a document we are looking for * @param projection specifies which projection MongoDB will return from the documents in the result set. * @return A document that satisfies the query specified as the argument to this method. * @mongodb.driver.manual tutorial/query-documents/ Querying */ @Nullable public DBObject findOne(final Object id, final DBObject projection) { return findOne(new BasicDBObject("_id", id), new DBCollectionFindOptions().projection(projection)); }
private DBCursor prepareCursor(final FindOptions findOptions) { final DBObject query = getQueryObject(); if (LOG.isTraceEnabled()) { LOG.trace(String.format("Running query(%s) : %s, options: %s,", dbColl.getName(), query, findOptions)); } if (findOptions.isSnapshot() && (findOptions.getSortDBObject() != null || findOptions.hasHint())) { LOG.warn("Snapshotted query should not have hint/sort."); } if (findOptions.getCursorType() != NonTailable && (findOptions.getSortDBObject() != null)) { LOG.warn("Sorting on tail is not allowed."); } return dbColl.find(query, findOptions.getOptions() .copy() .sort(getSortObject()) .projection(getFieldsObject())) .setDecoderFactory(ds.getDecoderFact()); }
/** * Get a single document from collection. * * @param query the selection criteria using query operators. * @param projection specifies which projection MongoDB will return from the documents in the result set. * @param sort A document whose fields specify the attributes on which to sort the result set. * @param readPreference {@code ReadPreference} to be used for this operation * @return A document that satisfies the query specified as the argument to this method. * @mongodb.driver.manual tutorial/query-documents/ Querying */ @Nullable public DBObject findOne(@Nullable final DBObject query, @Nullable final DBObject projection, @Nullable final DBObject sort, final ReadPreference readPreference) { return findOne(query != null ? query : new BasicDBObject(), new DBCollectionFindOptions().projection(projection).sort(sort).readPreference(readPreference)); }
/** * Initializes a new database cursor. * * @param collection collection to use * @param query the query filter to apply * @param fields keys to return from the query * @param readPreference the read preference for this query */ public DBCursor(final DBCollection collection, final DBObject query, @Nullable final DBObject fields, @Nullable final ReadPreference readPreference) { this(collection, query, new DBCollectionFindOptions().projection(fields).readPreference(readPreference)); addOption(collection.getOptions()); DBObject indexKeys = lookupSuitableHints(query, collection.getHintFields()); if (indexKeys != null) { hint(indexKeys); } }
/** * Copy this DBCollectionFindOptions instance into a new instance. * * @return the new DBCollectionFindOptions with the same settings as this instance. */ public DBCollectionFindOptions copy() { DBCollectionFindOptions copiedOptions = new DBCollectionFindOptions(); copiedOptions.batchSize(batchSize); copiedOptions.limit(limit); copiedOptions.modifiers(modifiers); copiedOptions.projection(projection); copiedOptions.maxTime(maxTimeMS, TimeUnit.MILLISECONDS); copiedOptions.maxAwaitTime(maxAwaitTimeMS, TimeUnit.MILLISECONDS); copiedOptions.skip(skip); copiedOptions.sort(sort); copiedOptions.cursorType(cursorType); copiedOptions.noCursorTimeout(noCursorTimeout); copiedOptions.oplogReplay(oplogReplay); copiedOptions.partial(partial); copiedOptions.readPreference(readPreference); copiedOptions.readConcern(readConcern); copiedOptions.collation(collation); copiedOptions.comment(comment); copiedOptions.hint(hint); copiedOptions.max(max); copiedOptions.min(min); copiedOptions.returnKey(returnKey); copiedOptions.showRecordId(showRecordId); return copiedOptions; }
/** * Sets a document describing the fields to return for all matching documents. * * @param projection the project document, which may be null. * @return this * @mongodb.driver.manual reference/method/db.collection.find/ Projection */ FindOptions projection(final DBObject projection) { options.projection(projection); return this; }
/** * Get a single document from collection by '_id'. * * @param id value of '_id' field of a document we are looking for * @param projection specifies which projection MongoDB will return from the documents in the result set. * @return A document that satisfies the query specified as the argument to this method. * @mongodb.driver.manual tutorial/query-documents/ Querying */ @Nullable public DBObject findOne(final Object id, final DBObject projection) { return findOne(new BasicDBObject("_id", id), new DBCollectionFindOptions().projection(projection)); }
/** * Get a single document from collection. * * @param query the selection criteria using query operators. * @param projection specifies which projection MongoDB will return from the documents in the result set. * @param sort A document whose fields specify the attributes on which to sort the result set. * @param readPreference {@code ReadPreference} to be used for this operation * @return A document that satisfies the query specified as the argument to this method. * @mongodb.driver.manual tutorial/query-documents/ Querying */ @Nullable public DBObject findOne(@Nullable final DBObject query, @Nullable final DBObject projection, @Nullable final DBObject sort, final ReadPreference readPreference) { return findOne(query != null ? query : new BasicDBObject(), new DBCollectionFindOptions().projection(projection).sort(sort).readPreference(readPreference)); }
/** * Initializes a new database cursor. * * @param collection collection to use * @param query the query filter to apply * @param fields keys to return from the query * @param readPreference the read preference for this query */ public DBCursor(final DBCollection collection, final DBObject query, @Nullable final DBObject fields, @Nullable final ReadPreference readPreference) { this(collection, query, new DBCollectionFindOptions().projection(fields).readPreference(readPreference)); addOption(collection.getOptions()); DBObject indexKeys = lookupSuitableHints(query, collection.getHintFields()); if (indexKeys != null) { hint(indexKeys); } }
private DBCursor prepareCursor(final FindOptions findOptions) { final DBObject query = getQueryObject(); if (LOG.isTraceEnabled()) { LOG.trace(String.format("Running query(%s) : %s, options: %s,", dbColl.getName(), query, findOptions)); } if (findOptions.isSnapshot() && (findOptions.getSortDBObject() != null || findOptions.hasHint())) { LOG.warning("Snapshotted query should not have hint/sort."); } if (findOptions.getCursorType() != NonTailable && (findOptions.getSortDBObject() != null)) { LOG.warning("Sorting on tail is not allowed."); } return dbColl.find(query, findOptions.getOptions() .copy() .sort(getSortObject()) .projection(getFieldsObject())) .setDecoderFactory(ds.getDecoderFact()); }
@Override public List<T> results(){ DBObject projection; if(fieldsSpecified){ projection = fields; }else{ projection = dao.getKeyFields(); } DBCollectionFindOptions options = new DBCollectionFindOptions(); options.projection(projection); if(maxTimeMS > 0){ options.maxTime(maxTimeMS, TimeUnit.MILLISECONDS); } if(orderBy != null){ options.sort(SortUtil.getSort(orderBy)); } if(pageNumber>0 && pageSize>0){ options.skip((pageNumber-1) * pageSize); options.limit(pageSize); } DBCollection coll = dao.getCollection(); DBCursor cursor = coll.find(condition, options); return MapperUtil.toList(dao.getEntityClass(), cursor, withoutCascade); }
/** * Copy this DBCollectionFindOptions instance into a new instance. * * @return the new DBCollectionFindOptions with the same settings as this instance. */ public DBCollectionFindOptions copy() { DBCollectionFindOptions copiedOptions = new DBCollectionFindOptions(); copiedOptions.batchSize(batchSize); copiedOptions.limit(limit); copiedOptions.modifiers(modifiers); copiedOptions.projection(projection); copiedOptions.maxTime(maxTimeMS, TimeUnit.MILLISECONDS); copiedOptions.maxAwaitTime(maxAwaitTimeMS, TimeUnit.MILLISECONDS); copiedOptions.skip(skip); copiedOptions.sort(sort); copiedOptions.cursorType(cursorType); copiedOptions.noCursorTimeout(noCursorTimeout); copiedOptions.oplogReplay(oplogReplay); copiedOptions.partial(partial); copiedOptions.readPreference(readPreference); copiedOptions.readConcern(readConcern); copiedOptions.collation(collation); copiedOptions.comment(comment); copiedOptions.hint(hint); copiedOptions.max(max); copiedOptions.min(min); copiedOptions.returnKey(returnKey); copiedOptions.showRecordId(showRecordId); return copiedOptions; }