// matches all documents where 'age' field has value as 30
collection.find(eq("age", 30));
public final class Filters
extends java.lang.Object
A helper class to create all type of Filter
s.
Modifier and Type | Field and Description |
---|---|
static Filter |
ALL
A filter to select all elements.
|
Modifier and Type | Method and Description |
---|---|
static Filter |
and(Filter... filters)
Creates an and filter which performs a logical AND operation on two filters and selects
the documents that satisfy both filters.
|
static Filter |
elemMatch(java.lang.String field,
Filter filter)
Creates an element match filter that matches documents that contain an array
value with at least one element that matches the specified
filter . |
static Filter |
eq(java.lang.String field,
java.lang.Object value)
Creates an equality filter which matches documents where the value
of a field equals the specified value.
|
static Filter |
gt(java.lang.String field,
java.lang.Object value)
Creates a greater than filter which matches those documents where the value
of the value is greater than (i.e.
|
static Filter |
gte(java.lang.String field,
java.lang.Object value)
Creates a greater equal filter which matches those documents where the value
of the value is greater than or equals to (i.e.
|
static Filter |
in(java.lang.String field,
java.lang.Object... values)
Creates an in filter which matches the documents where
the value of a field equals any value in the specified array.
|
static Filter |
lt(java.lang.String field,
java.lang.Object value)
Creates a lesser than filter which matches those documents where the value
of the value is less than (i.e.
|
static Filter |
lte(java.lang.String field,
java.lang.Object value)
Creates a lesser equal filter which matches those documents where the value
of the value is lesser than or equals to (i.e.
|
static Filter |
not(Filter filter)
Creates a not filter which performs a logical NOT operation on a
filter and selects
the documents that do not satisfy the filter . |
static Filter |
or(Filter... filters)
Creates an or filter which performs a logical OR operation on two filters and selects
the documents that satisfy at least one of the filter.
|
static Filter |
regex(java.lang.String field,
java.lang.String value)
Creates a string filter which provides regular expression capabilities
for pattern matching strings in documents.
|
static Filter |
text(java.lang.String field,
java.lang.String value)
Creates a text filter which performs a text search on the content of the fields
indexed with a full-text index.
|
public static final Filter ALL
A filter to select all elements.
public static Filter eq(java.lang.String field, java.lang.Object value)
Creates an equality filter which matches documents where the value of a field equals the specified value.
// matches all documents where 'age' field has value as 30
collection.find(eq("age", 30));
field
- the valuevalue
- the valuepublic static Filter and(Filter... filters)
Creates an and filter which performs a logical AND operation on two filters and selects the documents that satisfy both filters.
// matches all documents where 'age' field has value as 30 and
// 'name' field has value as John Doe
collection.find(and(eq("age", 30), eq("name", "John Doe")));
filters
- operand filterspublic static Filter or(Filter... filters)
Creates an or filter which performs a logical OR operation on two filters and selects the documents that satisfy at least one of the filter.
// matches all documents where 'age' field has value as 30 or
// 'name' field has value as John Doe
collection.find(or(eq("age", 30), eq("name", "John Doe")));
filters
- operand filterspublic static Filter not(Filter filter)
Creates a not filter which performs a logical NOT operation on a filter
and selects
the documents that do not satisfy the filter
. This also includes documents
that do not contain the value.
// matches all documents where 'age' field has value not equals to 30
collection.find(not(eq("age", 30)));
filter
- the filterpublic static Filter gt(java.lang.String field, java.lang.Object value)
Creates a greater than filter which matches those documents where the value of the value is greater than (i.e. >) the specified value.
// matches all documents where 'age' field has value greater than 30
collection.find(gt("age", 30));
field
- the valuevalue
- the valuepublic static Filter gte(java.lang.String field, java.lang.Object value)
Creates a greater equal filter which matches those documents where the value of the value is greater than or equals to (i.e. >=) the specified value.
// matches all documents where 'age' field has value greater than or equal to 30
collection.find(gte("age", 30));
field
- the valuevalue
- the valuepublic static Filter lt(java.lang.String field, java.lang.Object value)
Creates a lesser than filter which matches those documents where the value of the value is less than (i.e. <) the specified value.
// matches all documents where 'age' field has value less than 30
collection.find(lt("age", 30));
field
- the valuevalue
- the valuepublic static Filter lte(java.lang.String field, java.lang.Object value)
Creates a lesser equal filter which matches those documents where the value of the value is lesser than or equals to (i.e. ⇐) the specified value.
// matches all documents where 'age' field has value lesser than or equal to 30
collection.find(lte("age", 30));
field
- the valuevalue
- the valuepublic static Filter text(java.lang.String field, java.lang.String value)
Creates a text filter which performs a text search on the content of the fields indexed with a full-text index.
// matches all documents where 'address' field has value 'roads'.
collection.find(text("address", "roads"));
field
- the valuevalue
- the text valueTextIndexingService
,
TextTokenizer
public static Filter regex(java.lang.String field, java.lang.String value)
Creates a string filter which provides regular expression capabilities for pattern matching strings in documents.
// matches all documents where 'name' value starts with 'jim' or 'joe'.
collection.find(regex("name", "^(jim|joe).*"));
field
- the valuevalue
- the regular expressionpublic static Filter in(java.lang.String field, java.lang.Object... values)
Creates an in filter which matches the documents where the value of a field equals any value in the specified array.
// matches all documents where 'age' field has value in [20, 30, 40]
collection.find(in("age", 20, 30, 40));
field
- the valuevalues
- the range valuespublic static Filter elemMatch(java.lang.String field, Filter filter)
Creates an element match filter that matches documents that contain an array
value with at least one element that matches the specified filter
.
// matches all documents which has an array field - 'color' and the array
// contains a value - 'red'.
collection.find(elemMatch("color", eq("$", "red"));
field
- the valuefilter
- the filter to satisfy