> This page location: /docs.md > /docs/drivers.md > /docs/languages/java.md > /docs/drivers/java/sync/current.md > /docs/drivers/java/sync/current/builders.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

# Filters Builders

## Overview

In this guide, you can learn how to use **builders** to specify **filters** for your queries in the MongoDB Java driver.

Builders are classes provided by the MongoDB Java driver that help you construct [BSON](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/document-data-format-bson.md#std-label-bson) objects. To learn more, see our [guide on builders.](https://www.mongodb.com/docs/drivers/java/sync/current/builders.md#std-label-java-builders)

Filters are the operations MongoDB uses to limit your results to what you want to see.

Some places we use filters are:

- `find()`

- match stage of aggregation

- `deleteOne()`/`deleteMany()`

- `updateOne()`/`updateMany()`

Some examples of results from queries with filters are:

- Items that cost $0 to $25

- A hotel with amenities that include an indoor swimming pool and free parking

- A food critic review that mentions "spicy"

This guide shows you how to use builders with examples of the following types of operators:

- [Comparison](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-comparison)

- [Logical](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-logical)

- [Arrays](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-arrays)

- [Elements](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-elements)

- [Evaluation](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-evaluation)

- [Bitwise](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-bitwise)

- [Geospatial](https://www.mongodb.com/docs/drivers/java/sync/current/builders/filters.md#std-label-geospatial)

The `Filters` class provides static factory methods for all the MongoDB query operators. Each method returns an instance of the [BSON](https://www.mongodb.com/docs/drivers/java/sync/current/data-formats/document-data-format-bson.md#std-label-bson) type, which you can pass to any method that expects a query filter.

**Tip:**

For brevity, you can choose to import all methods of the [Filters](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html) class statically:

```java
import static com.mongodb.client.model.Filters.*;
```

The following examples assume this static import.

The Filter examples in this guide use the following sample collections:

Collection: `paint_purchases`

```json
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] }
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 3, "color": "blue", "qty": 8, "vendor": ["B", "A"] }
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"] }
{ "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }
{ "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
{ "_id": 7, "color": "green", "qty": 8,"vendor": ["B", "C"] }
{ "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
```

Collection: `binary_numbers`

```json
{ "_id": 9, "a": 54, "binaryValue": "00110110" }
{ "_id": 10, "a": 20, "binaryValue": "00010100" }
{ "_id": 11, "a": 68, "binaryValue": "1000100" }
{ "_id": 12, "a": 102, "binaryValue": "01100110" }
```

Collection: `geo_points`

```json
{ "_id": 13, "coordinates": { "type": "Point", "coordinates": [2.0, 2.0] } }
{ "_id": 14, "coordinates": { "type": "Point", "coordinates": [5.0, 6.0] } }
{ "_id": 15, "coordinates": { "type": "Point", "coordinates": [1.0, 3.0] } }
{ "_id": 16, "coordinates": { "type": "Point", "coordinates": [4.0, 7.0] } }
```

## Comparison

The comparison filters include all operators that compare the value in a document to a specified value.

The comparison operator methods include:

| Comparison Method | Matches |
| --- | --- |
| [eq()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#eq(java.lang.String,TItem)) | values equal to a specified value. |
| [gt()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#gt(java.lang.String,TItem)) | values greater than a specified value. |
| [gte()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#gte(java.lang.String,TItem)) | values greater than or equal to a specified value. |
| [lt()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#lt(java.lang.String,TItem)) | values less than a specified value. |
| [lte()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#lte(java.lang.String,TItem)) | values less than or equal to a specified value. |
| [ne()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#ne(java.lang.String,TItem)) | values not equal to a specified value. |
| [in()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#in(java.lang.String,java.lang.Iterable)) | any of the values specified in an array. |
| [nin()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#nin(java.lang.String,java.lang.Iterable)) | none of the values specified in an array. |
| [empty()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#empty()) | all the documents. |

The following example creates a filter that matches all documents where the value of the `qty` field equals "5" in the `paint_purchases` collection:

```java
Bson equalComparison = eq("qty", 5);
collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] }
{ "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
```

The following example creates a filter that matches all documents where the value of the `qty` field is greater than or equal to "10" in the `paint_purchases` collection:

```java
Bson gteComparison = gte("qty", 10);
collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }
```

The following example creates a filter that matches all documents in the `paint_purchases` collection because the predicate is empty:

```java
Bson emptyComparison = empty();
collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The output of the preceding query consists of all the documents in the collection.

```json
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] }
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 3, "color": "blue", "qty": 8, "vendor": ["B", "A"] }
...
```

## Logical

The logical operators perform logical operations based on the conditions of the specified method.

The logical operator methods include:

| Logical Method | Matches |
| --- | --- |
| [and()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#and(java.lang.Iterable)) | documents with the conditions of all the filters. This operator joins filters with a logical `AND`. |
| [or()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#or(org.bson.conversions.Bson...)) | documents with the conditions of either filter. This operator joins filters with a logical `OR`. |
| [not()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#not(org.bson.conversions.Bson)) | documents that do not match the filter. |
| [nor()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#nor(org.bson.conversions.Bson...)) | documents that fail to match both filters. This operator joins filters with a logical `NOR`. |

The following example creates a filter that matches documents where the value of the `qty` field is greater than "8" or the value of the `color` field equals "pink" in the `paint_purchases` collection:

```java
Bson orComparison = or(gt("qty", 8), eq("color", "pink"));
collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }
{ "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
```

## Arrays

The array operators evaluate the array field in a document.

The array operator methods include:

| Array Method | Matches |
| --- | --- |
| [all()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#all(java.lang.String,java.lang.Iterable)) | documents if the array field contains every element specified in the query. |
| [elemMatch()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#elemMatch(java.lang.String,org.bson.conversions.Bson)) | documents if an element in the array field matches all the specified conditions. |
| [size()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#size(java.lang.String,int)) | documents if the array field is a specified number of elements. |

The following example matches documents with a `vendors` array containing both "A" and "D" in the `paint_purchases` collection:

```java
List<String> search = Arrays.asList("A", "D");
Bson allComparison = all("vendor", search);
collection.find(allComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
```

## Elements

The elements operators evaluate the nature of a specified field.

The elements operator methods include:

| Elements Method | Matches |
| --- | --- |
| [exists()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#exists(java.lang.String)) | documents that have the specified field. |
| [type()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#type(java.lang.String,java.lang.String)) | documents if a field is of the specified type. |

The following example matches documents that have a `qty` field and its value does not equal "5" or "8" in the `paint_purchases` collection:

```java
Bson existsComparison = and(exists("qty"), nin("qty", 5, 8));
collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"]}
{ "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }
{ "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
```

## Evaluation

The evaluation operators evaluate the value of any field in a document.

The evaluation operator methods include:

| Evaluation Method | Matches |
| --- | --- |
| [mod()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#mod(java.lang.String,long,long)) | documents where a modulo operation on a field value produces a specified result. |
| [regex()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#regex(java.lang.String,java.lang.String)) | documents where values contain a specified regular expression. |
| [text()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#text(java.lang.String)) | documents which contain a specified full-text search expression. |
| [where()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#where(java.lang.String)) | documents which contain a specified JavaScript expression. |

The following example matches documents that have a `color` field starting with the letter "p" in the `paint_purchases` collection:

```java
Bson regexComparison = regex("color", "^p");
collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
```

## Bitwise

The bitwise operators convert a number into its binary value to evaluate its bits.

The bitwise operator methods include:

| Bitwise Method | Matches |
| --- | --- |
| [bitsAllSet()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#bitsAllSet(java.lang.String,long)) | documents where the specified bits of a field are set (i.e. "1"). |
| [bitsAllClear()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#bitsAllClear(java.lang.String,long)) | documents where the specified bits of a field are clear (i.e. "0"). |
| [bitsAnySet()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#bitsAnySet(java.lang.String,long)) | documents where at least one of the specified bits of a field are set (i.e. "1"). |
| [bitsAnyClear()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#bitsAnyClear(java.lang.String,long)) | documents where at least one of the specified bits of a field are clear (i.e. "0"). |

The following example matches documents that have a `bitField` field with bits set at positions of the corresponding bitmask "34" (i.e. "00100010") in the `binary_numbers` collection:

```java
Bson bitsComparison = bitsAllSet("a", 34);
collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 9, "a": 54, "binaryValue": "00110110" }
{ "_id": 12, "a": 102, "binaryValue": "01100110" }
```

## Geospatial

The geospatial operators evaluate a specified coordinate and its relation to a shape or location.

The geospatial operator methods include:

| Geospatial Method | Matches |
| --- | --- |
| [geoWithin()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoWithin(java.lang.String,org.bson.conversions.Bson)) | documents containing a GeoJSON geometry value that falls within a bounding GeoJSON geometry. |
| [geoWithinBox()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoWithinBox(java.lang.String,double,double,double,double)) | documents containing a coordinates value that exist within the specified box. |
| [geoWithinPolygon()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoWithinPolygon(java.lang.String,java.util.List)) | documents containing a coordinates value that exist within the specified polygon. |
| [geoWithinCenter()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoWithinCenter(java.lang.String,double,double,double)) | documents containing a coordinates value that exist within the specified circle. |
| [geoWithinCenterSphere()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoWithinCenterSphere(java.lang.String,double,double,double)) | geometries containing a geospatial data value (GeoJSON or legacy coordinate pairs) that exist within the specified circle, using spherical geometry. |
| [geoIntersects()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#geoIntersects(java.lang.String,org.bson.conversions.Bson)) | geometries that intersect with a GeoJSON geometry. The `2dsphere` index supports `$geoIntersects`. |
| [near()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#near(java.lang.String,org.bson.conversions.Bson,java.lang.Double,java.lang.Double)) | geospatial objects in proximity to a point. Requires a geospatial index. The `2dsphere` and `2d` indexes support `$near`. |
| [nearSphere()](https://mongodb.github.io/mongo-java-driver/5.14/apidocs/driver-core/com/mongodb/client/model/Filters.html#nearSphere(java.lang.String,org.bson.conversions.Bson,java.lang.Double,java.lang.Double)) | geospatial objects in proximity to a point on a sphere. Requires a geospatial index. The `2dsphere` and `2d` indexes support `$nearSphere`. |

The following example creates a filter that matches documents in which the `point` field contains a GeoJSON geometry that falls within the given [Polygon](https://www.mongodb.com/docs/manual/reference/geojson/#polygon) in the `geo_points` collection:

```java
Polygon square = new Polygon(Arrays.asList(new Position(0, 0), 
                                    new Position(4, 0), 
                                    new Position(4, 4), 
                                    new Position(0, 4),
                                    new Position(0, 0)));

// Prints documents that contain "coordinates" values that are within the bounds of the polygon passed as the filter parameter
Bson geoWithinComparison = geoWithin("coordinates", square);
collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson()));
```

The following shows the output of the preceding query:

```json
{ "_id": 13, "coordinates": {"type": "Point", "coordinates": [2.0, 2.0]} }
{ "_id": 15, "coordinates": {"type": "Point", "coordinates": [1.0, 3.0]} }
```
