Package com.mongodb.client.model
Class Filters
java.lang.Object
com.mongodb.client.model.Filters
A factory for query filters. A convenient way to use this class is to statically import all of its methods, which allows usage like:
collection.find(and(eq("x", 1), lt("y", 3)));
- Since:
- 3.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <TItem> Bson
Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.static <TItem> Bson
Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.static Bson
Creates a filter that performs a logical AND of the provided list of filters.static Bson
Creates a filter that performs a logical AND of the provided list of filters.static Bson
bitsAllClear
(String fieldName, long bitmask) Creates a filter that matches all documents where all of the bit positions are clear in the field.static Bson
bitsAllSet
(String fieldName, long bitmask) Creates a filter that matches all documents where all of the bit positions are set in the field.static Bson
bitsAnyClear
(String fieldName, long bitmask) Creates a filter that matches all documents where any of the bit positions are clear in the field.static Bson
bitsAnySet
(String fieldName, long bitmask) Creates a filter that matches all documents where any of the bit positions are set in the field.static Bson
Creates a filter that matches all documents containing a field that is an array where at least one member of the array matches the given filter.static Bson
empty()
Creates an empty filter that will match all documents.static <TItem> Bson
Creates a filter that matches all documents where the value of the field name equals the specified value.static <TItem> Bson
eq
(TItem value) Creates a filter that matches all documents where the value of _id field equals the specified value.static Bson
Creates a filter that matches all documents that contain the given field.static Bson
Creates a filter that matches all documents that either contain or do not contain the given field, depending on the value of the exists parameter.static <TExpression>
Bsonexpr
(TExpression expression) Allows the use of aggregation expressions within the query language.static Bson
geoIntersects
(String fieldName, Geometry geometry) Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.static Bson
geoIntersects
(String fieldName, Bson geometry) Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.static Bson
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.static Bson
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.static Bson
geoWithinBox
(String fieldName, double lowerLeftX, double lowerLeftY, double upperRightX, double upperRightY) Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified box.static Bson
geoWithinCenter
(String fieldName, double x, double y, double radius) Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified circle.static Bson
geoWithinCenterSphere
(String fieldName, double x, double y, double radius) Creates a filter that matches all documents containing a field with geospatial data (GeoJSON or legacy coordinate pairs) that exist entirely within the specified circle, using spherical geometry.static Bson
geoWithinPolygon
(String fieldName, List<List<Double>> points) Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified polygon.static <TItem> Bson
Creates a filter that matches all documents where the value of the given field is greater than the specified value.static <TItem> Bson
Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.static <TItem> Bson
Creates a filter that matches all documents where the value of a field equals any value in the list of specified values.static <TItem> Bson
Creates a filter that matches all documents where the value of a field equals any value in the list of specified values.static Bson
jsonSchema
(Bson schema) Creates a filter that matches all documents that validate against the given JSON schema document.static <TItem> Bson
Creates a filter that matches all documents where the value of the given field is less than the specified value.static <TItem> Bson
Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.static Bson
Creates a filter that matches all documents where the value of a field divided by a divisor has the specified remainder (i.e.static <TItem> Bson
Creates a filter that matches all documents where the value of the field name does not equal the specified value.static Bson
Creates a filter that matches all documents containing a field with geospatial data that is near the specified point.static Bson
Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point.static Bson
Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point.static Bson
nearSphere
(String fieldName, double x, double y, Double maxDistance, Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified point using spherical geometry.static Bson
nearSphere
(String fieldName, Point geometry, Double maxDistance, Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point using spherical geometry.static Bson
nearSphere
(String fieldName, Bson geometry, Double maxDistance, Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point using spherical geometry.static <TItem> Bson
Creates a filter that matches all documents where the value of a field does not equal any of the specified values or does not exist.static <TItem> Bson
Creates a filter that matches all documents where the value of a field does not equal any of the specified values or does not exist.static Bson
Creates a filter that performs a logical NOR operation on all the specified filters.static Bson
Creates a filter that performs a logical NOR operation on all the specified filters.static Bson
Creates a filter that matches all documents that do not match the passed in filter.static Bson
Creates a filter that preforms a logical OR of the provided list of filters.static Bson
Creates a filter that preforms a logical OR of the provided list of filters.static Bson
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern.static Bson
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern with the given options applied.static Bson
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern.static Bson
Creates a filter that matches all documents where the value of a field is an array of the specified size.static Bson
Creates a filter that matches all documents matching the given search term.static Bson
text
(String search, TextSearchOptions textSearchOptions) Creates a filter that matches all documents matching the given the search term with the given text search options.static Bson
Creates a filter that matches all documents where the value of the field is of the specified BSON type.static Bson
Creates a filter that matches all documents where the value of the field is of the specified BSON type.static Bson
Creates a filter that matches all documents for which the given expression is true.
-
Method Details
-
eq
Creates a filter that matches all documents where the value of _id field equals the specified value. Note that this doesn't actually generate a $eq operator, as the query language doesn't require it.- Type Parameters:
TItem
- the value type- Parameters:
value
- the value, which may be null- Returns:
- the filter
- Since:
- 3.4
- MongoDB documentation
- $eq
-
eq
Creates a filter that matches all documents where the value of the field name equals the specified value. Note that this doesn't actually generate a $eq operator, as the query language doesn't require it.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value, which may be null- Returns:
- the filter
- MongoDB documentation
- $eq
-
ne
Creates a filter that matches all documents where the value of the field name does not equal the specified value.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value, which may be null- Returns:
- the filter
- MongoDB documentation
- $ne
-
gt
Creates a filter that matches all documents where the value of the given field is greater than the specified value.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value- Returns:
- the filter
- MongoDB documentation
- $gt
-
lt
Creates a filter that matches all documents where the value of the given field is less than the specified value.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value- Returns:
- the filter
- MongoDB documentation
- $lt
-
gte
Creates a filter that matches all documents where the value of the given field is greater than or equal to the specified value.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value- Returns:
- the filter
- MongoDB documentation
- $gte
-
lte
Creates a filter that matches all documents where the value of the given field is less than or equal to the specified value.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalue
- the value- Returns:
- the filter
- MongoDB documentation
- $lte
-
in
Creates a filter that matches all documents where the value of a field equals any value in the list of specified values.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $in
-
in
Creates a filter that matches all documents where the value of a field equals any value in the list of specified values.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $in
-
nin
Creates a filter that matches all documents where the value of a field does not equal any of the specified values or does not exist.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $nin
-
nin
Creates a filter that matches all documents where the value of a field does not equal any of the specified values or does not exist.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $nin
-
and
Creates a filter that performs a logical AND of the provided list of filters.
will generate a MongoDB query like:and(eq("x", 1), lt("y", 3))
{ $and: [{x : 1}, {y : {$lt : 3}}]}
- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $and
-
and
Creates a filter that performs a logical AND of the provided list of filters.
will generate a MongoDB query like:and(eq("x", 1), lt("y", 3))
{ $and: [{x : 1}, {y : {$lt : 3}}]}
- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $and
-
or
Creates a filter that preforms a logical OR of the provided list of filters.- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $or
-
or
Creates a filter that preforms a logical OR of the provided list of filters.- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $or
-
not
Creates a filter that matches all documents that do not match the passed in filter. Requires the field name to passed as part of the value passed in and lifts it to create a valid "$not" query:
will generate a MongoDB query like:not(eq("x", 1))
{x : $not: {$eq : 1}}
- Parameters:
filter
- the value- Returns:
- the filter
- MongoDB documentation
- $not
-
nor
Creates a filter that performs a logical NOR operation on all the specified filters.- Parameters:
filters
- the list of values- Returns:
- the filter
- MongoDB documentation
- $nor
-
nor
Creates a filter that performs a logical NOR operation on all the specified filters.- Parameters:
filters
- the list of values- Returns:
- the filter
- MongoDB documentation
- $nor
-
exists
Creates a filter that matches all documents that contain the given field.- Parameters:
fieldName
- the field name- Returns:
- the filter
- MongoDB documentation
- $exists
-
exists
Creates a filter that matches all documents that either contain or do not contain the given field, depending on the value of the exists parameter.- Parameters:
fieldName
- the field nameexists
- true to check for existence, false to check for absence- Returns:
- the filter
- MongoDB documentation
- $exists
-
type
Creates a filter that matches all documents where the value of the field is of the specified BSON type.- Parameters:
fieldName
- the field nametype
- the BSON type- Returns:
- the filter
- MongoDB documentation
- $type
-
type
Creates a filter that matches all documents where the value of the field is of the specified BSON type.- Parameters:
fieldName
- the field nametype
- the string representation of the BSON type- Returns:
- the filter
- MongoDB documentation
- $type
-
mod
Creates a filter that matches all documents where the value of a field divided by a divisor has the specified remainder (i.e. perform a modulo operation to select documents).- Parameters:
fieldName
- the field namedivisor
- the modulusremainder
- the remainder- Returns:
- the filter
- MongoDB documentation
- $mod
-
regex
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern.- Parameters:
fieldName
- the field namepattern
- the pattern- Returns:
- the filter
- MongoDB documentation
- $regex
-
regex
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern with the given options applied.- Parameters:
fieldName
- the field namepattern
- the patternoptions
- the options- Returns:
- the filter
- MongoDB documentation
- $regex
-
regex
Creates a filter that matches all documents where the value of the field matches the given regular expression pattern.- Parameters:
fieldName
- the field namepattern
- the pattern- Returns:
- the filter
- MongoDB documentation
- $regex
-
text
Creates a filter that matches all documents matching the given search term.- Parameters:
search
- the search term- Returns:
- the filter
- MongoDB documentation
- $text
-
text
Creates a filter that matches all documents matching the given the search term with the given text search options.- Parameters:
search
- the search termtextSearchOptions
- the text search options to use- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $text
-
where
Creates a filter that matches all documents for which the given expression is true.- Parameters:
javaScriptExpression
- the JavaScript expression- Returns:
- the filter
- MongoDB documentation
- $where
-
expr
Allows the use of aggregation expressions within the query language. -
all
Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $all
-
all
Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.- Type Parameters:
TItem
- the value type- Parameters:
fieldName
- the field namevalues
- the list of values- Returns:
- the filter
- MongoDB documentation
- $all
-
elemMatch
Creates a filter that matches all documents containing a field that is an array where at least one member of the array matches the given filter.- Parameters:
fieldName
- the field namefilter
- the filter to apply to each element- Returns:
- the filter
- MongoDB documentation
- $elemMatch
-
size
Creates a filter that matches all documents where the value of a field is an array of the specified size.- Parameters:
fieldName
- the field namesize
- the size of the array- Returns:
- the filter
- MongoDB documentation
- $size
-
bitsAllClear
Creates a filter that matches all documents where all of the bit positions are clear in the field.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllClear
- Since server release
- 3.2
-
bitsAllSet
Creates a filter that matches all documents where all of the bit positions are set in the field.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllSet
- Since server release
- 3.2
-
bitsAnyClear
Creates a filter that matches all documents where any of the bit positions are clear in the field.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllClear
- Since server release
- 3.2
-
bitsAnySet
Creates a filter that matches all documents where any of the bit positions are set in the field.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAnySet
- Since server release
- 3.2
-
geoWithin
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry object- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- Since server release
- 2.4
-
geoWithin
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry object- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- Since server release
- 2.4
-
geoWithinBox
public static Bson geoWithinBox(String fieldName, double lowerLeftX, double lowerLeftY, double upperRightX, double upperRightY) Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified box.- Parameters:
fieldName
- the field namelowerLeftX
- the lower left x coordinate of the boxlowerLeftY
- the lower left y coordinate of the boxupperRightX
- the upper left x coordinate of the boxupperRightY
- the upper left y coordinate of the box- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- $box
- Since server release
- 2.4
-
geoWithinPolygon
Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified polygon.- Parameters:
fieldName
- the field namepoints
- a list of pairs of x, y coordinates. Any extra dimensions are ignored- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- $polygon
- Since server release
- 2.4
-
geoWithinCenter
Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified circle.- Parameters:
fieldName
- the field namex
- the x coordinate of the circley
- the y coordinate of the circleradius
- the radius of the circle, as measured in the units used by the coordinate system- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- $center
- Since server release
- 2.4
-
geoWithinCenterSphere
Creates a filter that matches all documents containing a field with geospatial data (GeoJSON or legacy coordinate pairs) that exist entirely within the specified circle, using spherical geometry. If using longitude and latitude, specify longitude first.- Parameters:
fieldName
- the field namex
- the x coordinate of the circley
- the y coordinate of the circleradius
- the radius of the circle, in radians- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoWithin
- $centerSphere
- Since server release
- 2.4
-
geoIntersects
Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry object- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoIntersects
- Since server release
- 2.4
-
geoIntersects
Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry object- Returns:
- the filter
- Since:
- 3.1
- MongoDB documentation
- $geoIntersects
- Since server release
- 2.4
-
near
public static Bson near(String fieldName, Point geometry, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry objectmaxDistance
- the maximum distance from the point, in meters. It may be null.minDistance
- the minimum distance from the point, in meters. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
near
public static Bson near(String fieldName, Bson geometry, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry objectmaxDistance
- the maximum distance from the point, in meters. It may be null.minDistance
- the minimum distance from the point, in meters. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
near
public static Bson near(String fieldName, double x, double y, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified point.- Parameters:
fieldName
- the field namex
- the x coordinatey
- the y coordinatemaxDistance
- the maximum distance from the point, in radians. It may be null.minDistance
- the minimum distance from the point, in radians. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
nearSphere
public static Bson nearSphere(String fieldName, Point geometry, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point using spherical geometry.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry objectmaxDistance
- the maximum distance from the point, in meters. It may be null.minDistance
- the minimum distance from the point, in meters. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
nearSphere
public static Bson nearSphere(String fieldName, Bson geometry, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point using spherical geometry.- Parameters:
fieldName
- the field namegeometry
- the bounding GeoJSON geometry objectmaxDistance
- the maximum distance from the point, in meters. It may be null.minDistance
- the minimum distance from the point, in meters. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
nearSphere
public static Bson nearSphere(String fieldName, double x, double y, @Nullable Double maxDistance, @Nullable Double minDistance) Creates a filter that matches all documents containing a field with geospatial data that is near the specified point using spherical geometry.- Parameters:
fieldName
- the field namex
- the x coordinatey
- the y coordinatemaxDistance
- the maximum distance from the point, in radians. It may be null.minDistance
- the minimum distance from the point, in radians. It may be null.- Returns:
- the filter
- Since:
- 3.1
-
jsonSchema
Creates a filter that matches all documents that validate against the given JSON schema document.- Parameters:
schema
- the JSON schema to validate against- Returns:
- the filter
- Since:
- 3.6
- MongoDB documentation
- $jsonSchema
- Since server release
- 3.6
-
empty
Creates an empty filter that will match all documents.- Returns:
- the filter
- Since:
- 4.2
-