/** * Returns the collation options * * @return the collation options * @mongodb.server.release 3.4 */ public Collation getCollation() { return options.getCollation(); }
/** * Gets the hint to apply. * * @return the hint, which should describe an existing */ public String getHint() { return options.getHintString(); }
/** * Gets the limit to apply. The default is 0, which means there is no limit. * * @return the limit * @mongodb.driver.manual reference/method/cursor.limit/#cursor.limit Limit */ public int getLimit() { return options.getLimit(); }
/** * Get the count of documents in collection that would match a criteria. * * @param query specifies the selection criteria * @param projection this is ignored * @param limit limit the count to this value * @param skip number of documents to skip * @param readPreference {@link ReadPreference} to be used for this operation * @return the number of documents that matches selection criteria * @throws MongoException if the operation failed * @mongodb.driver.manual reference/command/count/ Count * @deprecated Prefer {@link #count(DBObject, DBCollectionCountOptions)} */ @Deprecated public long getCount(@Nullable final DBObject query, @Nullable final DBObject projection, final long limit, final long skip, final ReadPreference readPreference) { return getCount(query, new DBCollectionCountOptions().limit(limit).skip(skip).readPreference(readPreference)); }
private DBCollectionCountOptions getDbCollectionCountOptions() { DBCollectionCountOptions countOptions = new DBCollectionCountOptions() .readPreference(getReadPreferenceForCursor()) .readConcern(getReadConcern()) .collation(getCollation()) .maxTime(findOptions.getMaxTime(MILLISECONDS), MILLISECONDS); Object hint = findOptions.getHint() != null ? findOptions.getHint() : findOptions.getModifiers().get("$hint"); if (hint != null) { if (hint instanceof String) { countOptions.hintString((String) hint); } else { countOptions.hint((DBObject) hint); } } return countOptions; } }
notNull("countOptions", options); CountOperation operation = new CountOperation(getNamespace()) .skip(options.getSkip()) .limit(options.getLimit()) .maxTime(options.getMaxTime(MILLISECONDS), MILLISECONDS) .collation(options.getCollation()); if (query != null) { operation.filter(wrap(query)); DBObject hint = options.getHint(); if (hint != null) { operation.hint(wrap(hint)); } else { String hintString = options.getHintString(); if (hintString != null) { operation.hint(new BsonString(hintString)); ReadPreference optionsReadPreference = options.getReadPreference(); ReadConcern optionsReadConcern = options.getReadConcern(); return executor.execute(operation, optionsReadPreference != null ? optionsReadPreference : getReadPreference(), optionsReadConcern != null ? optionsReadConcern : getReadConcern());
/** * Counts the number of objects matching the query this does take limit/skip into consideration * * @return the number of objects * @throws MongoException if the operation failed * @see #count() */ public int size() { DBCollectionCountOptions countOptions = getDbCollectionCountOptions().skip(findOptions.getSkip()).limit(findOptions.getLimit()); return (int) collection.getCount(getQuery(), countOptions); }
/** * Gets the maximum execution time on the server for this operation. The default is 0, which places no limit on the execution time. * * @param timeUnit the time unit to return the result in * @return the maximum execution time in the given time unit */ public long getMaxTime(final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); return options.getMaxTime(timeUnit); }
/** * Gets the number of documents to skip. The default is 0. * * @return the number of documents to skip * @mongodb.driver.manual reference/method/cursor.skip/#cursor.skip Skip */ public int getSkip() { return options.getSkip(); }
/** * Returns the readConcern * * @return the readConcern * @mongodb.server.release 3.2 */ public ReadConcern getReadConcern() { return options.getReadConcern(); }
/** * Returns the readPreference * * @return the readPreference */ public ReadPreference getReadPreference() { return options.getReadPreference(); }
/** * Sets the collation * * @param collation the collation * @return this * @mongodb.server.release 3.4 */ public CountOptions collation(final Collation collation) { options.collation(collation); return this; }
/** * Sets the hint to apply. * * @param hint the name of the index which should be used for the operation * @return this */ public CountOptions hint(final String hint) { options.hintString(hint); return this; }
/** * Sets the limit to apply. * * @param limit the limit * @return this * @mongodb.driver.manual reference/method/cursor.limit/#cursor.limit Limit */ public CountOptions limit(final int limit) { options.limit(limit); return this; }
@Test public void passThrough() { Collation collation = Collation.builder() .locale("en") .caseLevel(true) .build(); DBCollectionCountOptions options = new CountOptions() .collation(collation) .hint("i'm a hint") .limit(18) .maxTime(15, TimeUnit.MINUTES) .readPreference(ReadPreference.secondaryPreferred()) .readConcern(ReadConcern.LOCAL) .skip(12) .getOptions(); assertEquals(collation, options.getCollation()); assertEquals("i'm a hint", options.getHintString()); assertEquals(18, options.getLimit()); assertEquals(15, options.getMaxTime(TimeUnit.MINUTES)); assertEquals(ReadPreference.secondaryPreferred(), options.getReadPreference()); assertEquals(ReadConcern.LOCAL, options.getReadConcern()); assertEquals(12, options.getSkip()); } }
private DBCollectionCountOptions getDbCollectionCountOptions() { DBCollectionCountOptions countOptions = new DBCollectionCountOptions() .readPreference(getReadPreferenceForCursor()) .readConcern(getReadConcern()) .collation(getCollation()) .maxTime(findOptions.getMaxTime(MILLISECONDS), MILLISECONDS); Object hint = findOptions.getHint() != null ? findOptions.getHint() : findOptions.getModifiers().get("$hint"); if (hint != null) { if (hint instanceof String) { countOptions.hintString((String) hint); } else { countOptions.hint((DBObject) hint); } } return countOptions; } }
/** * Get the count of documents in collection that would match a criteria. * * @param query specifies the selection criteria * @param projection this is ignored * @param limit limit the count to this value * @param skip number of documents to skip * @param readPreference {@link ReadPreference} to be used for this operation * @return the number of documents that matches selection criteria * @throws MongoException if the operation failed * @mongodb.driver.manual reference/command/count/ Count * @deprecated Prefer {@link #count(DBObject, DBCollectionCountOptions)} */ @Deprecated public long getCount(@Nullable final DBObject query, @Nullable final DBObject projection, final long limit, final long skip, final ReadPreference readPreference) { return getCount(query, new DBCollectionCountOptions().limit(limit).skip(skip).readPreference(readPreference)); }
/** * Counts the number of objects matching the query this does take limit/skip into consideration * * @return the number of objects * @throws MongoException if the operation failed * @see #count() */ public int size() { DBCollectionCountOptions countOptions = getDbCollectionCountOptions().skip(findOptions.getSkip()).limit(findOptions.getLimit()); return (int) collection.getCount(getQuery(), countOptions); }
/** * Gets the maximum execution time on the server for this operation. The default is 0, which places no limit on the execution time. * * @param timeUnit the time unit to return the result in * @return the maximum execution time in the given time unit */ public long getMaxTime(final TimeUnit timeUnit) { notNull("timeUnit", timeUnit); return options.getMaxTime(timeUnit); }
/** * Gets the number of documents to skip. The default is 0. * * @return the number of documents to skip * @mongodb.driver.manual reference/method/cursor.skip/#cursor.skip Skip */ public int getSkip() { return options.getSkip(); }