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)));
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.
Creates a filter that matches all documents where the value of the field name equals the specified value.
Creates a filter that matches all documents that contain the given field.
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.
Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.
Creates a filter that matches all documents containing a field with geospatial data that intersects with the specified shape.
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.
Creates a filter that matches all documents containing a field with geospatial data that exists entirely within the specified shape.
Creates a filter that matches all documents containing a field with grid coordinates data that exist entirely within the specified
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 <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,
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
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
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
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 <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
(
Bson
... filters)
Creates a filter that performs a logical NOR operation on all the specified filters.
static
Bson
nor
(
Iterable
<
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
(
Bson
... filters)
Creates a filter that preforms a logical OR of the provided list of filters.
static
Bson
or
(
Iterable
<
Bson
> filters)
Creates a filter that preforms a logical OR of the provided list of filters.
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
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
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,
String
language)
Deprecated.
public 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. Note that this does
actually generate a $eq operator, as the query language doesn't require it.
Type Parameters:
TItem
- the value type
Parameters:
fieldName
- the field name
value
- the value
Returns:
the filter
-
MongoDB documentation
public 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.
-
Type Parameters:
-
TItem
- the value type
-
Parameters:
-
fieldName
- the field name
-
value
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
value
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
value
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
value
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
value
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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:
and(eq("x", 1), lt("y", 3))
will generate a MongoDB query like:
{x : 1, y : {$lt : 3}}
-
Parameters:
-
filters
- the list of filters to and together
-
Returns:
-
the filter
-
MongoDB documentation
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:
and(eq("x", 1), lt("y", 3))
will generate a MongoDB query like:
{x : 1, y : {$lt : 3}}
-
Parameters:
-
filters
- the list of filters to and together
-
Returns:
-
the filter
-
MongoDB documentation
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
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
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:
not(eq("x", 1))
will generate a MongoDB query like:
{x : $not: {$eq : 1}}
-
Parameters:
-
filter
- the value
-
Returns:
-
the filter
-
MongoDB documentation
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
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
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
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 name
-
exists
- true to check for existence, false to check for absence
-
Returns:
-
the filter
-
MongoDB documentation
-
$exists
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 name
-
type
- the BSON type
-
Returns:
-
the filter
-
MongoDB documentation
-
$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 name
-
type
- the string representation of the BSON type
-
Returns:
-
the filter
-
MongoDB documentation
-
$type
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 name
-
divisor
- the modulus
-
remainder
- the remainder
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
pattern
- the pattern
-
Returns:
-
the filter
-
MongoDB documentation
-
$regex
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.
-
Parameters:
-
fieldName
- the field name
-
pattern
- the pattern
-
options
- the options
-
Returns:
-
the filter
-
MongoDB documentation
-
$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 name
-
pattern
- the pattern
-
Returns:
-
the filter
-
MongoDB documentation
-
$regex
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
public static
Bson
text(
String
search,
String
language)
Deprecated.
Creates a filter that matches all documents matching the given search term using the given language.
-
Parameters:
-
search
- the search term
-
language
- the language to use for stop words
-
Returns:
-
the filter
-
MongoDB documentation
-
$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 term
-
textSearchOptions
- the text search options to use
-
Returns:
-
the filter
-
Since:
-
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
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
values
- the list of values
-
Returns:
-
the filter
-
MongoDB documentation
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 name
-
filter
- the filter to apply to each element
-
Returns:
-
the filter
-
MongoDB documentation
-
$elemMatch
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 name
-
size
- the size of the array
-
Returns:
-
the filter
-
MongoDB documentation
-
$size
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 name
-
bitmask
- the bitmask
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$bitsAllClear
-
Since server release
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 name
-
bitmask
- the bitmask
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$bitsAllSet
-
Since server release
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 name
-
bitmask
- the bitmask
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$bitsAllClear
-
Since server release
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 name
-
bitmask
- the bitmask
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$bitsAnySet
-
Since server release
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 name
-
geometry
- the bounding GeoJSON geometry object
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
Since server release
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 name
-
geometry
- the bounding GeoJSON geometry object
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
Since server release
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
-
Parameters:
-
fieldName
- the field name
-
lowerLeftX
- the lower left x coordinate of the box
-
lowerLeftY
- the lower left y coordinate of the box
-
upperRightX
- the upper left x coordinate of the box
-
upperRightY
- the upper left y coordinate of the box
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
Since server release
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 name
-
points
- a list of pairs of x, y coordinates. Any extra dimensions are ignored
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
$polygon
-
Since server release
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 name
-
x
- the x coordinate of the circle
-
y
- the y coordinate of the circle
-
radius
- the radius of the circle, as measured in the units used by the coordinate system
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
$center
-
Since server release
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 name
-
x
- the x coordinate of the circle
-
y
- the y coordinate of the circle
-
radius
- the radius of the circle, in radians
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoWithin
-
$centerSphere
-
Since server release
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 name
-
geometry
- the bounding GeoJSON geometry object
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoIntersects
-
Since server release
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 name
-
geometry
- the bounding GeoJSON geometry object
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$geoIntersects
-
Since server release
Double
maxDistance,
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 name
-
geometry
- the bounding GeoJSON geometry object
-
maxDistance
- the maximum distance from the point, in meters
-
minDistance
- the minimum distance from the point, in meters
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release
Double
maxDistance,
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 name
-
geometry
- the bounding GeoJSON geometry object
-
maxDistance
- the maximum distance from the point, in meters
-
minDistance
- the minimum distance from the point, in meters
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release
Double
maxDistance,
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 name
-
x
- the x coordinate
-
y
- the y coordinate
-
maxDistance
- the maximum distance from the point, in radians
-
minDistance
- the minimum distance from the point, in radians
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release
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.
-
Parameters:
-
fieldName
- the field name
-
geometry
- the bounding GeoJSON geometry object
-
maxDistance
- the maximum distance from the point, in meters
-
minDistance
- the minimum distance from the point, in meters
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release
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.
-
Parameters:
-
fieldName
- the field name
-
geometry
- the bounding GeoJSON geometry object
-
maxDistance
- the maximum distance from the point, in meters
-
minDistance
- the minimum distance from the point, in meters
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release
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.
-
Parameters:
-
fieldName
- the field name
-
x
- the x coordinate
-
y
- the y coordinate
-
maxDistance
- the maximum distance from the point, in radians
-
minDistance
- the minimum distance from the point, in radians
-
Returns:
-
the filter
-
Since:
-
MongoDB documentation
-
$near
-
Since server release