Class Criteria
java.lang.Object
org.springframework.data.mongodb.core.query.Criteria
- All Implemented Interfaces:
CriteriaDefinition
- Direct Known Subclasses:
GridFsCriteria
Central class for creating queries. It follows a fluent API style so that you can easily chain together multiple
criteria. Static import of the
Criteria.where
method improves readability.- Author:
- Thomas Risberg, Oliver Gierke, Thomas Darimont, Christoph Strobl, Mark Paluch, Andreas Zink, Ziemowit Stolarczyk, Clément Petit, James McNee
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
MongoDB specific bitwise query operators like$bitsAllClear, $bitsAllSet,...
for usage withbits()
andQuery
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a criterion using the given object as a pattern.Creates a criterion using the $all operator.all
(Collection<?> values) Creates a criterion using the $all operator.Static factory method to create a Criteria using the provided keyCreates a criterion ($jsonSchema
) matching documents against a given structure defined by theMongoJsonSchema
.andOperator
(Collection<Criteria> criteria) Creates a criteria using the$and
operator for all of the provided criteria.andOperator
(Criteria... criteria) Creates a criteria using the$and
operator for all of the provided criteria.bits()
UseCriteria.BitwiseCriteriaOperators
as gateway to create a criterion using one of the bitwise operators like$bitsAllClear
.static Criteria
Static factory method to create aCriteria
matching an example object.static Criteria
Static factory method to create aCriteria
matching an example object.Creates a criterion using the $elemMatch operatorboolean
exists
(boolean value) Creates a criterion using the $exists operator.org.bson.Document
GetDocument
representation.getKey()
Get the identifying key.protected org.bson.Document
Creates a criterion using the $gt operator.Creates a criterion using the $gte operator.int
hashCode()
Creates a criterion using the $in operator.in
(Collection<?> values) Creates a criterion using the $in operator.intersects
(GeoJson geoJson) Creates criterion using$geoIntersects
operator which matches intersections of the givengeoJson
structure and the documents one.Creates a criterion using equalityisNull()
Creates a criterion using null equality comparison which matches documents that either contain the item field whose value is null or that do not contain the item field.Creates a criterion using aBsonType
comparison which matches only documents that contain the item field whose value is equal toBsonType.NULL
.Creates a criterion using the $lt operator.Creates a criterion using the $lte operator.static Criteria
Static factory method to create aCriteria
matching documents against a given structure defined by theMongoJsonSchema
using ($jsonSchema
) operator.maxDistance
(double maxDistance) Creates a geo-spatial criterion using a $maxDistance operation, for use with $nearminDistance
(double minDistance) Creates a geospatial criterion using a $minDistance operation, for use with $near or $nearSphere.Creates a criterion using the $mod operator.Creates a criterion using the $ne operator.Creates a geospatial criterion using a $near operation.nearSphere
(Point point) Creates a geospatial criterion using a $nearSphere operation.Creates a criterion using the $nin operator.nin
(Collection<?> values) Creates a criterion using the $nin operator.norOperator
(Collection<Criteria> criteria) Creates a criteria using the$nor
operator for all of the provided criteria.norOperator
(Criteria... criteria) Creates a criteria using the$nor
operator for all of the provided criteria.not()
Creates a criterion using the $not meta operator which affects the clause directly followingorOperator
(Collection<Criteria> criteria) Creates a criteria using the$or
operator for all of the provided criteria.orOperator
(Criteria... criteria) Creates a criteria using the$or
operator for all of the provided criteria.Creates a criterion using a $regex operator.Creates a criterion using a $regex and $options operator.Syntactical sugar foris(Object)
making obvious that we create a regex predicate.regex
(org.bson.BsonRegularExpression regex) Use a MongoDB nativeBsonRegularExpression
.sampleRate
(double sampleRate) Creates a criterion using the $sampleRate operator.size
(int size) Creates a criterion using the $size operator.type
(int typeNumber) Creates a criterion using the $type operator.type
(Collection<JsonSchemaObject.Type> types) Creates a criterion using the $type operator.type
(JsonSchemaObject.Type... types) Creates a criterion using the $type operator.static Criteria
Static factory method to create a Criteria using the provided keyCreates a geospatial criterion using a $geoWithin operation.withinSphere
(Circle circle) Creates a geospatial criterion using a $geoWithin $centerSphere operation.
-
Constructor Details
-
Criteria
public Criteria() -
Criteria
-
Criteria
-
-
Method Details
-
where
Static factory method to create a Criteria using the provided key- Parameters:
key
- the property or field name.- Returns:
- new instance of
Criteria
.
-
byExample
Static factory method to create aCriteria
matching an example object.- Parameters:
example
- must not be null.- Returns:
- new instance of
Criteria
. - Since:
- 1.8
- See Also:
-
byExample
Static factory method to create aCriteria
matching an example object.
By default theExample
uses typed matching restricting it to probe assignable types. For example, when sticking with the default type key (_class
), the query has restrictions such as_class : { $in : [com.acme.Person] }
.
To avoid the above mentioned type restriction use anUntypedExampleMatcher
withExample.of(Object, org.springframework.data.domain.ExampleMatcher)
.- Parameters:
example
- must not be null.- Returns:
- new instance of
Criteria
. - Since:
- 1.8
- See Also:
-
matchingDocumentStructure
Static factory method to create aCriteria
matching documents against a given structure defined by theMongoJsonSchema
using ($jsonSchema
) operator.- Parameters:
schema
- must not be null.- Returns:
- this
- Since:
- 2.1
- See Also:
-
and
Static factory method to create a Criteria using the provided key- Returns:
- new instance of
Criteria
.
-
is
Creates a criterion using equality- Parameters:
value
- can be null.- Returns:
- this.
-
isNull
Creates a criterion using null equality comparison which matches documents that either contain the item field whose value is null or that do not contain the item field.
UseisNullValue()
to only query for documents that contain the field whose value is equal toBsonType.NULL
.
Useexists(boolean)
to query for documents that do (not) contain the field.- Returns:
- this.
- Since:
- 3.3
- See Also:
-
isNullValue
Creates a criterion using aBsonType
comparison which matches only documents that contain the item field whose value is equal toBsonType.NULL
.
UseisNull()
to query for documents that contain the field with a null value or do not contain the field at all.
Useexists(boolean)
to query for documents that do (not) contain the field.- Returns:
- this.
- Since:
- 3.3
- See Also:
-
ne
Creates a criterion using the $ne operator.- Parameters:
value
- can be null.- Returns:
- this.
- See Also:
-
lt
Creates a criterion using the $lt operator.- Parameters:
value
- must not be null.- Returns:
- this.
- See Also:
-
lte
Creates a criterion using the $lte operator.- Parameters:
value
- must not be null.- Returns:
- this.
- See Also:
-
gt
Creates a criterion using the $gt operator.- Parameters:
value
- must not be null.- Returns:
- this.
- See Also:
-
gte
Creates a criterion using the $gte operator.- Parameters:
value
- can be null.- Returns:
- this.
- See Also:
-
in
Creates a criterion using the $in operator.- Parameters:
values
- the values to match against- Returns:
- this.
- See Also:
-
in
Creates a criterion using the $in operator.- Parameters:
values
- the collection containing the values to match against- Returns:
- this.
- See Also:
-
nin
Creates a criterion using the $nin operator.- Parameters:
values
-- Returns:
- this.
- See Also:
-
nin
Creates a criterion using the $nin operator.- Parameters:
values
- must not be null.- Returns:
- this.
- See Also:
-
mod
Creates a criterion using the $mod operator.- Parameters:
value
- must not be null.remainder
- must not be null.- Returns:
- this.
- See Also:
-
all
Creates a criterion using the $all operator.- Parameters:
values
- must not be null.- Returns:
- this.
- See Also:
-
all
Creates a criterion using the $all operator.- Parameters:
values
- must not be null.- Returns:
- this.
- See Also:
-
size
Creates a criterion using the $size operator.- Parameters:
size
-- Returns:
- this.
- See Also:
-
exists
Creates a criterion using the $exists operator.- Parameters:
value
-- Returns:
- this.
- See Also:
-
sampleRate
Creates a criterion using the $sampleRate operator.- Parameters:
sampleRate
- sample rate to determine number of documents to be randomly selected from the input. Must be between0
and1
.- Returns:
- this.
- Since:
- 3.3
- See Also:
-
type
Creates a criterion using the $type operator.- Parameters:
typeNumber
-- Returns:
- this.
- See Also:
-
type
Creates a criterion using the $type operator.- Parameters:
types
- must not be null.- Returns:
- this.
- Since:
- 2.1
- See Also:
-
type
Creates a criterion using the $type operator.- Parameters:
types
- must not be null.- Returns:
- this.
- Since:
- 3.2
- See Also:
-
not
Creates a criterion using the $not meta operator which affects the clause directly following- Returns:
- this.
- See Also:
-
regex
Creates a criterion using a $regex operator.- Parameters:
regex
- must not be null.- Returns:
- this.
- See Also:
-
regex
Creates a criterion using a $regex and $options operator.- Parameters:
regex
- must not be null.options
- can be null.- Returns:
- this.
- See Also:
-
regex
Syntactical sugar foris(Object)
making obvious that we create a regex predicate.- Parameters:
pattern
- must not be null.- Returns:
- this.
-
regex
Use a MongoDB nativeBsonRegularExpression
.- Parameters:
regex
- must not be null.- Returns:
- this.
-
withinSphere
Creates a geospatial criterion using a $geoWithin $centerSphere operation. This is only available for Mongo 2.4 and higher.- Parameters:
circle
- must not be null- Returns:
- this.
- See Also:
-
within
Creates a geospatial criterion using a $geoWithin operation.- Parameters:
shape
- must not be null.- Returns:
- this.
- See Also:
-
near
Creates a geospatial criterion using a $near operation.- Parameters:
point
- must not be null- Returns:
- this.
- See Also:
-
nearSphere
Creates a geospatial criterion using a $nearSphere operation. This is only available for Mongo 1.7 and higher.- Parameters:
point
- must not be null- Returns:
- this.
- See Also:
-
intersects
Creates criterion using$geoIntersects
operator which matches intersections of the givengeoJson
structure and the documents one. Requires MongoDB 2.4 or better.- Parameters:
geoJson
- must not be null.- Returns:
- this.
- Since:
- 1.8
-
maxDistance
Creates a geo-spatial criterion using a $maxDistance operation, for use with $near- Parameters:
maxDistance
-- Returns:
- this.
- See Also:
-
minDistance
Creates a geospatial criterion using a $minDistance operation, for use with $near or $nearSphere.- Parameters:
minDistance
-- Returns:
- this.
- Since:
- 1.7
-
elemMatch
Creates a criterion using the $elemMatch operator- Parameters:
criteria
- must not be null.- Returns:
- this.
- See Also:
-
alike
Creates a criterion using the given object as a pattern.- Parameters:
sample
- must not be null.- Returns:
- this.
- Since:
- 1.8
-
andDocumentStructureMatches
Creates a criterion ($jsonSchema
) matching documents against a given structure defined by theMongoJsonSchema
.
NOTE:$jsonSchema
cannot be used on field/property level but defines the whole document structure. Please useMongoJsonSchema.MongoJsonSchemaBuilder.properties(JsonSchemaProperty...)
to specify nested fields or query them using the$type
operator.- Parameters:
schema
- must not be null.- Returns:
- this
- Since:
- 2.1
- See Also:
-
bits
UseCriteria.BitwiseCriteriaOperators
as gateway to create a criterion using one of the bitwise operators like$bitsAllClear
.- Returns:
- new instance of
Criteria.BitwiseCriteriaOperators
. Never null. - Since:
- 2.1
-
orOperator
Creates a criteria using the$or
operator for all of the provided criteria.Note that MongoDB doesn't support an
$nor
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.
-
orOperator
Creates a criteria using the$or
operator for all of the provided criteria.Note that MongoDB doesn't support an
$nor
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.- Since:
- 3.2
-
norOperator
Creates a criteria using the$nor
operator for all of the provided criteria.Note that MongoDB doesn't support an
$nor
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.
-
norOperator
Creates a criteria using the$nor
operator for all of the provided criteria.Note that MongoDB doesn't support an
$nor
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.- Since:
- 3.2
-
andOperator
Creates a criteria using the$and
operator for all of the provided criteria.Note that MongoDB doesn't support an
$and
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.
-
andOperator
Creates a criteria using the$and
operator for all of the provided criteria.Note that MongoDB doesn't support an
$and
operator to be wrapped in a$not
operator.- Parameters:
criteria
- must not be null.- Returns:
- this.
- Throws:
IllegalArgumentException
- if this method follows anot()
call directly.- Since:
- 3.2
-
getKey
Description copied from interface:CriteriaDefinition
Get the identifying key.- Specified by:
getKey
in interfaceCriteriaDefinition
- Returns:
- can be null.
-
getCriteriaObject
public org.bson.Document getCriteriaObject()Description copied from interface:CriteriaDefinition
GetDocument
representation.- Specified by:
getCriteriaObject
in interfaceCriteriaDefinition
- Returns:
- never null.
-
getSingleCriteriaObject
protected org.bson.Document getSingleCriteriaObject() -
equals
-
hashCode
public int hashCode()
-