java.lang.Object
org.springframework.data.mongodb.core.query.Query
- All Implemented Interfaces:
ReadConcernAware,ReadPreferenceAware
- Direct Known Subclasses:
BasicQuery,TextQuery
MongoDB Query object representing criteria, projection, sorting and query hints.
- Author:
- Thomas Risberg, Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Anton Barkan
-
Constructor Summary
ConstructorsConstructorDescriptionQuery()Query(CriteriaDefinition criteriaDefinition) Creates a newQueryusing the givenCriteriaDefinition. -
Method Summary
Modifier and TypeMethodDescriptionaddCriteria(CriteriaDefinition criteriaDefinition) Adds the givenCriteriaDefinitionto the currentQuery.allowDiskUse(boolean allowDiskUse) Enables writing to temporary files for aggregation stages and queries.Allows querying of a replica.Set theCollationapplying language-specific rules for string comparison.Add a comment to the query that is propagated to the profile log.cursorBatchSize(int batchSize) Set the number of documents to return in each response batch.booleanexhaust()fields()Get theCollationdefining language-specific rules for string comparison.protected List<CriteriaDefinition>org.bson.DocumentgetHint()intgetLimit()Get the maximum number of documents to be return.getMeta()org.bson.Documentcom.mongodb.ReadConcerncom.mongodb.ReadPreferencelonggetSkip()Get the number of documents to skip.org.bson.DocumentinthashCode()booleanbooleanbooleanbooleanReturns whether the query islimited.static booleanDeprecated.don't call this method as the restricted type handling will undergo some significant changes going forward.booleanisSorted()Returns true if theQueryhas a sort parameter.limit(int limit) Limit the number of returned documents tolimit.Limit the number of returned documents toLimit.maxTimeMsec(long maxTimeMsec) static QueryCreate an independent copy of the givenQuery.static Queryquery(CriteriaDefinition criteriaDefinition) Static factory method to create aQueryusing the providedCriteriaDefinition.protected booleanquerySettingsEquals(Query that) Tests whether the settings of the givenQueryare equal to this query.Restricts the query to only return documents instances that are exactly of the given types.voidskip(long skip) Set number of documents to skip before returning results.toString()with(KeysetScrollPosition position) Sets the given cursor position on theQueryinstance.with(OffsetScrollPosition position) Sets the given cursor position on theQueryinstance.Sets the given pagination information on theQueryinstance.with(ScrollPosition position) Sets the given cursor position on theQueryinstance.Configures the query to use the given hint when being executed.withHint(org.bson.Document hint) Configures the query to use the givenhintwhen being executed.withReadConcern(com.mongodb.ReadConcern readConcern) Configures the query to use the givenReadConcernwhen being executed.withReadPreference(com.mongodb.ReadPreference readPreference) Configures the query to use the givenReadPreferencewhen being executed.
-
Constructor Details
-
Query
public Query() -
Query
Creates a newQueryusing the givenCriteriaDefinition.- Parameters:
criteriaDefinition- must not be null.- Since:
- 1.6
-
-
Method Details
-
query
Static factory method to create aQueryusing the providedCriteriaDefinition.- Parameters:
criteriaDefinition- must not be null.- Returns:
- new instance of
Query. - Since:
- 1.6
-
addCriteria
Adds the givenCriteriaDefinitionto the currentQuery.- Parameters:
criteriaDefinition- must not be null.- Returns:
- this.
- Since:
- 1.6
-
fields
-
skip
Set number of documents to skip before returning results. Use zero or a negative value to avoid skipping.- Parameters:
skip- number of documents to skip. Use zero or a negative value to avoid skipping.- Returns:
- this.
-
limit
Limit the number of returned documents tolimit. A zero or negative value is considered as unlimited.- Parameters:
limit- number of documents to return. Use zero or negative for unlimited.- Returns:
- this.
-
limit
Limit the number of returned documents toLimit.- Parameters:
limit- number of documents to return.- Returns:
- this.
- Since:
- 4.2
-
withHint
Configures the query to use the given hint when being executed. Thehintcan either be an index name or a jsonDocumentrepresentation.- Parameters:
hint- must not be null or empty.- Returns:
- this.
- See Also:
-
Document.parse(String)
-
withReadConcern
Configures the query to use the givenReadConcernwhen being executed.- Parameters:
readConcern- must not be null.- Returns:
- this.
- Since:
- 3.1
-
withReadPreference
Configures the query to use the givenReadPreferencewhen being executed.- Parameters:
readPreference- must not be null.- Returns:
- this.
- Since:
- 4.1
-
hasReadConcern
public boolean hasReadConcern()- Specified by:
hasReadConcernin interfaceReadConcernAware- Returns:
- true if a
ReadConcernis set.
-
getReadConcern
public com.mongodb.ReadConcern getReadConcern()- Specified by:
getReadConcernin interfaceReadConcernAware- Returns:
- the
ReadConcernto apply or null if none set.
-
hasReadPreference
public boolean hasReadPreference()- Specified by:
hasReadPreferencein interfaceReadPreferenceAware- Returns:
- true if a
ReadPreferenceis set.
-
getReadPreference
public com.mongodb.ReadPreference getReadPreference()- Specified by:
getReadPreferencein interfaceReadPreferenceAware- Returns:
- the
ReadPreferenceto apply or null if none set.
-
withHint
Configures the query to use the givenhintwhen being executed.- Parameters:
hint- must not be null.- Returns:
- this.
- Since:
- 2.2
-
with
Sets the given pagination information on theQueryinstance. Will transparently setskipandlimitas well as applying theSortinstance defined with thePageable.- Parameters:
pageable- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQueryinstance. Will transparently setskip.- Parameters:
position- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQueryinstance. Will transparently setskip.- Parameters:
position- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQueryinstance. Will transparently resetskip.- Parameters:
position- must not be null.- Returns:
- this.
-
hasKeyset
public boolean hasKeyset() -
getKeyset
-
with
- Parameters:
sort- must not be null.- Returns:
- this.
-
getRestrictedTypes
- Returns:
- the restrictedTypes
-
restrict
Restricts the query to only return documents instances that are exactly of the given types.- Parameters:
type- may not be nulladditionalTypes- may not be null- Returns:
- this.
-
getQueryObject
public org.bson.Document getQueryObject()- Returns:
- the query
Document.
-
getFieldsObject
public org.bson.Document getFieldsObject()- Returns:
- the field
Document.
-
getSortObject
public org.bson.Document getSortObject()- Returns:
- the sort
Document.
-
isSorted
public boolean isSorted()Returns true if theQueryhas a sort parameter.- Returns:
- true if sorted.
- Since:
- 2.2
- See Also:
-
getSkip
public long getSkip()Get the number of documents to skip. Zero or a negative value indicates no skip.- Returns:
- number of documents to skip
-
isLimited
public boolean isLimited()Returns whether the query islimited.- Returns:
trueif the query is limited;falseotherwise.- Since:
- 4.1
-
getLimit
public int getLimit()Get the maximum number of documents to be return. Zero or a negative value indicates no limit.- Returns:
- number of documents to return.
- See Also:
-
getHint
- Returns:
- can be null.
-
maxTimeMsec
- Parameters:
maxTimeMsec-- Returns:
- this.
- Since:
- 1.6
- See Also:
-
maxTime
- Parameters:
timeout- must not be null.- Returns:
- this.
- Since:
- 2.1
- See Also:
-
comment
Add a comment to the query that is propagated to the profile log.- Parameters:
comment- must not be null.- Returns:
- this.
- Since:
- 1.6
- See Also:
-
allowDiskUse
Enables writing to temporary files for aggregation stages and queries. When set to true, aggregation stages can write data to the_tmpsubdirectory in thedbPathdirectory.Starting in MongoDB 4.2, the profiler log messages and diagnostic log messages includes a
usedDiskindicator if any aggregation stage wrote data to temporary files due to memory restrictions.- Parameters:
allowDiskUse-- Returns:
- this.
- Since:
- 3.2
- See Also:
-
cursorBatchSize
Set the number of documents to return in each response batch.
Use 0 (zero) for no limit. A negative limit closes the cursor after returning a single batch indicating to the server that the client will not ask for a subsequent one.- Parameters:
batchSize- The number of documents to return per batch.- Returns:
- this.
- Since:
- 2.1
- See Also:
-
noCursorTimeout
- Returns:
- this.
- Since:
- 1.10
- See Also:
-
exhaust
- Returns:
- this.
- Since:
- 1.10
- See Also:
-
allowSecondaryReads
Allows querying of a replica.- Returns:
- this.
- Since:
- 3.0.2
- See Also:
-
partialResults
- Returns:
- this.
- Since:
- 1.10
- See Also:
-
getMeta
- Returns:
- never null.
- Since:
- 1.6
-
setMeta
- Parameters:
meta- must not be null.- Since:
- 1.6
-
collation
Set theCollationapplying language-specific rules for string comparison.- Parameters:
collation- can be null.- Returns:
- this.
- Since:
- 2.0
-
getCollation
Get theCollationdefining language-specific rules for string comparison.- Returns:
- never null.
- Since:
- 2.0
-
getCriteria
-
of
Create an independent copy of the givenQuery.
The resultingQuerywill not bebinary equalto the given source but semantically equal in terms of creating the same result when executed. -
toString
-
equals
-
querySettingsEquals
Tests whether the settings of the givenQueryare equal to this query.- Parameters:
that-- Returns:
-
hashCode
public int hashCode() -
isRestrictedTypeKey
Deprecated.don't call this method as the restricted type handling will undergo some significant changes going forward.Returns whether the given key is the one used to hold the type restriction information.- Parameters:
key-- Returns:
-