Class Query
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
ConstructorDescriptionQuery()
Query
(CriteriaDefinition criteriaDefinition) Creates a newQuery
using the givenCriteriaDefinition
. -
Method Summary
Modifier and TypeMethodDescriptionaddCriteria
(CriteriaDefinition criteriaDefinition) Adds the givenCriteriaDefinition
to the currentQuery
.allowDiskUse
(boolean allowDiskUse) Enables writing to temporary files for aggregation stages and queries.Allows querying of a replica.Set theCollation
applying 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.boolean
exhaust()
fields()
Get theCollation
defining language-specific rules for string comparison.protected List<CriteriaDefinition>
org.bson.Document
getHint()
int
getLimit()
Get the maximum number of documents to be return.getMeta()
org.bson.Document
com.mongodb.ReadConcern
com.mongodb.ReadPreference
long
getSkip()
Get the number of documents to skip.org.bson.Document
int
hashCode()
boolean
boolean
boolean
boolean
Returns whether the query islimited
.static boolean
Deprecated.don't call this method as the restricted type handling will undergo some significant changes going forward.boolean
isSorted()
Returns true if theQuery
has a sort parameter.limit
(int limit) Limit the number of returned documents tolimit
.Limit the number of returned documents toLimit
.maxTimeMsec
(long maxTimeMsec) static Query
Create an independent copy of the givenQuery
.static Query
query
(CriteriaDefinition criteriaDefinition) Static factory method to create aQuery
using the providedCriteriaDefinition
.protected boolean
querySettingsEquals
(Query that) Tests whether the settings of the givenQuery
are equal to this query.Restricts the query to only return documents instances that are exactly of the given types.void
skip
(long skip) Set number of documents to skip before returning results.toString()
with
(KeysetScrollPosition position) Sets the given cursor position on theQuery
instance.with
(OffsetScrollPosition position) Sets the given cursor position on theQuery
instance.Sets the given pagination information on theQuery
instance.with
(ScrollPosition position) Sets the given cursor position on theQuery
instance.Configures the query to use the given hint when being executed.withHint
(org.bson.Document hint) Configures the query to use the givenhint
when being executed.withReadConcern
(com.mongodb.ReadConcern readConcern) Configures the query to use the givenReadConcern
when being executed.withReadPreference
(com.mongodb.ReadPreference readPreference) Configures the query to use the givenReadPreference
when being executed.
-
Constructor Details
-
Query
public Query() -
Query
Creates a newQuery
using the givenCriteriaDefinition
.- Parameters:
criteriaDefinition
- must not be null.- Since:
- 1.6
-
-
Method Details
-
query
Static factory method to create aQuery
using the providedCriteriaDefinition
.- Parameters:
criteriaDefinition
- must not be null.- Returns:
- new instance of
Query
. - Since:
- 1.6
-
addCriteria
Adds the givenCriteriaDefinition
to 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. Thehint
can either be an index name or a jsonDocument
representation.- Parameters:
hint
- must not be null or empty.- Returns:
- this.
- See Also:
-
Document.parse(String)
-
withReadConcern
Configures the query to use the givenReadConcern
when being executed.- Parameters:
readConcern
- must not be null.- Returns:
- this.
- Since:
- 3.1
-
withReadPreference
Configures the query to use the givenReadPreference
when being executed.- Parameters:
readPreference
- must not be null.- Returns:
- this.
- Since:
- 4.1
-
hasReadConcern
public boolean hasReadConcern()- Specified by:
hasReadConcern
in interfaceReadConcernAware
- Returns:
- true if a
ReadConcern
is set.
-
getReadConcern
public com.mongodb.ReadConcern getReadConcern()- Specified by:
getReadConcern
in interfaceReadConcernAware
- Returns:
- the
ReadConcern
to apply or null if none set.
-
hasReadPreference
public boolean hasReadPreference()- Specified by:
hasReadPreference
in interfaceReadPreferenceAware
- Returns:
- true if a
ReadPreference
is set.
-
getReadPreference
public com.mongodb.ReadPreference getReadPreference()- Specified by:
getReadPreference
in interfaceReadPreferenceAware
- Returns:
- the
ReadPreference
to apply or null if none set.
-
withHint
Configures the query to use the givenhint
when being executed.- Parameters:
hint
- must not be null.- Returns:
- this.
- Since:
- 2.2
-
with
Sets the given pagination information on theQuery
instance. Will transparently setskip
andlimit
as well as applying theSort
instance defined with thePageable
.- Parameters:
pageable
- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQuery
instance. Will transparently setskip
.- Parameters:
position
- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQuery
instance. Will transparently setskip
.- Parameters:
position
- must not be null.- Returns:
- this.
-
with
Sets the given cursor position on theQuery
instance. 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 theQuery
has 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:
true
if the query is limited;false
otherwise.- 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_tmp
subdirectory in thedbPath
directory.Starting in MongoDB 4.2, the profiler log messages and diagnostic log messages includes a
usedDisk
indicator 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 theCollation
applying language-specific rules for string comparison.- Parameters:
collation
- can be null.- Returns:
- this.
- Since:
- 2.0
-
getCollation
Get theCollation
defining language-specific rules for string comparison.- Returns:
- never null.
- Since:
- 2.0
-
getCriteria
-
of
Create an independent copy of the givenQuery
.
The resultingQuery
will not bebinary equal
to the given source but semantically equal in terms of creating the same result when executed. -
toString
-
equals
-
querySettingsEquals
Tests whether the settings of the givenQuery
are 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:
-