Package com.mongodb.client.model
Class Filters
- java.lang.Object
-
- com.mongodb.client.model.Filters
-
public final class Filters extends Object
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
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <TItem> Bson
all(String fieldName, Iterable<TItem> values)
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
all(String fieldName, TItem... values)
Creates a filter that matches all documents where the value of a field is an array that contains all the specified values.static Bson
and(Iterable<Bson> filters)
Creates a filter that performs a logical AND of the provided list of filters.static Bson
and(Bson... filters)
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
elemMatch(String fieldName, Bson filter)
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 <TItem> Bson
eq(String fieldName, TItem value)
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
exists(String fieldName)
Creates a filter that matches all documents that contain the given field.static Bson
exists(String fieldName, boolean 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.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
geoWithin(String fieldName, Geometry geometry)
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.static Bson
geoWithin(String fieldName, Bson geometry)
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
gt(String fieldName, TItem value)
Creates a filter that matches all documents where the value of the given field is greater than the specified value.static <TItem> Bson
gte(String fieldName, TItem value)
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
in(String fieldName, Iterable<TItem> values)
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
in(String fieldName, TItem... values)
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
lt(String fieldName, TItem value)
Creates a filter that matches all documents where the value of the given field is less than the specified value.static <TItem> Bson
lte(String fieldName, TItem value)
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
mod(String fieldName, long divisor, long remainder)
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
ne(String fieldName, TItem value)
Creates a filter that matches all documents where the value of the field name does not equal the specified value.static Bson
near(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.static Bson
near(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.static Bson
near(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.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
nin(String fieldName, Iterable<TItem> values)
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
nin(String fieldName, TItem... values)
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
nor(Iterable<Bson> filters)
Creates a filter that performs a logical NOR operation on all the specified filters.static Bson
nor(Bson... filters)
Creates a filter that performs a logical NOR operation on all the specified filters.static Bson
not(Bson filter)
Creates a filter that matches all documents that do not match the passed in filter.static Bson
or(Iterable<Bson> filters)
Creates a filter that preforms a logical OR of the provided list of filters.static Bson
or(Bson... filters)
Creates a filter that preforms a logical OR of the provided list of filters.static Bson
regex(String fieldName, String pattern)
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
regex(String fieldName, String pattern, String options)
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
regex(String fieldName, Pattern pattern)
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
size(String fieldName, int size)
Creates a filter that matches all documents where the value of a field is an array of the specified size.static Bson
text(String search)
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
text(String search, String language)
Deprecated.static Bson
type(String fieldName, String type)
Creates a filter that matches all documents where the value of the field is of the specified BSON type.static Bson
type(String fieldName, BsonType type)
Creates a filter that matches all documents where the value of the field is of the specified BSON type.static Bson
where(String javaScriptExpression)
Creates a filter that matches all documents for which the given expression is true.
-
-
-
Method Detail
-
eq
public static <TItem> Bson eq(@Nullable TItem value)
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
public static <TItem> Bson eq(String fieldName, @Nullable TItem value)
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
public static <TItem> Bson ne(String fieldName, @Nullable TItem value)
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
public static <TItem> Bson gt(String fieldName, TItem value)
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
public static <TItem> Bson lt(String fieldName, TItem value)
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
public static <TItem> Bson gte(String fieldName, TItem value)
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
public static <TItem> Bson lte(String fieldName, TItem value)
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
public static <TItem> Bson in(String fieldName, TItem... values)
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
public static <TItem> Bson in(String fieldName, Iterable<TItem> values)
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
public static <TItem> Bson nin(String fieldName, TItem... values)
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
public static <TItem> Bson nin(String fieldName, Iterable<TItem> values)
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
public static Bson and(Iterable<Bson> filters)
Creates a filter that performs a logical AND of the provided list of filters. Note that this will only generate a "$and" operator if absolutely necessary, as the query language implicity ands together all the keys. In other words, a query expression like:
will generate a MongoDB query like:and(eq("x", 1), lt("y", 3))
{x : 1, y : {$lt : 3}}
- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $and
-
and
public static Bson and(Bson... filters)
Creates a filter that performs a logical AND of the provided list of filters. Note that this will only generate a "$and" operator if absolutely necessary, as the query language implicity ands together all the keys. In other words, a query expression like:
will generate a MongoDB query like:and(eq("x", 1), lt("y", 3))
{x : 1, y : {$lt : 3}}
- Parameters:
filters
- the list of filters to and together- Returns:
- the filter
- MongoDB documentation
- $and
-
or
public static Bson or(Iterable<Bson> filters)
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
public static Bson or(Bson... filters)
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
public static Bson not(Bson filter)
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
public static Bson nor(Bson... filters)
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
public static Bson nor(Iterable<Bson> filters)
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
public static Bson exists(String fieldName)
Creates a filter that matches all documents that contain the given field.- Parameters:
fieldName
- the field name- Returns:
- the filter
- MongoDB documentation
- $exists
-
exists
public static Bson exists(String fieldName, boolean 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
public static Bson type(String fieldName, BsonType 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
public static Bson type(String fieldName, String 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
public static Bson mod(String fieldName, long divisor, long remainder)
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
public static Bson regex(String fieldName, String pattern)
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 pattern- Returns:
- the filter
- MongoDB documentation
- $regex
-
regex
public static Bson regex(String fieldName, String pattern, @Nullable String options)
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
public static Bson regex(String fieldName, Pattern pattern)
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 pattern- Returns:
- the filter
- MongoDB documentation
- $regex
-
text
public static Bson text(String search)
Creates a filter that matches all documents matching the given search term.- Parameters:
search
- the search term- Returns:
- the filter
- MongoDB documentation
- $text
-
text
@Deprecated public static Bson text(String search, String language)
Deprecated. usetext(String, TextSearchOptions)
Creates a filter that matches all documents matching the given search term using the given language.- Parameters:
search
- the search termlanguage
- the language to use for stop words- Returns:
- the filter
- MongoDB documentation
- $text
-
text
public 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.- Parameters:
search
- the search termtextSearchOptions
- the text search options to use- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $text
-
where
public static Bson where(String javaScriptExpression)
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
public static <TExpression> Bson expr(TExpression expression)
Allows the use of aggregation expressions within the query language.
-
all
public static <TItem> Bson all(String fieldName, TItem... values)
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
public static <TItem> Bson all(String fieldName, Iterable<TItem> values)
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
public static Bson elemMatch(String fieldName, Bson filter)
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
public static Bson size(String fieldName, int 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
public 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.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllClear
- Since server release
- 3.2
-
bitsAllSet
public 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.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllSet
- Since server release
- 3.2
-
bitsAnyClear
public 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.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAllClear
- Since server release
- 3.2
-
bitsAnySet
public 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.- Parameters:
fieldName
- the field namebitmask
- the bitmask- Returns:
- the filter
- Since:
- 3.2
- MongoDB documentation
- $bitsAnySet
- Since server release
- 3.2
-
geoWithin
public static Bson geoWithin(String fieldName, Geometry geometry)
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
public static Bson geoWithin(String fieldName, Bson geometry)
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
public 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.- 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
public 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.- 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
public 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. 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
public 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.- 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
public 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.- 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, Double maxDistance, Double minDistance)
Creates a filter that matches all documents containing a field with geospatial data that is near the specified GeoJSON point.
-
near
public static Bson near(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.
-
near
public static Bson near(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.
-
nearSphere
public 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.
-
nearSphere
public 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.
-
nearSphere
public 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.
-
jsonSchema
public static Bson jsonSchema(Bson schema)
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
-
-