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

# Query for Null or Missing Fields

Query for `null` or missing fields in MongoDB using the following methods:

- Your programming language's driver.

- The [MongoDB Atlas UI](https://www.mongodb.com/docs/atlas/). To learn more, see [Query for Null or Missing Fields with MongoDB Atlas](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-query-null-documents-atlas-ui).

- [MongoDB Compass.](https://www.mongodb.com/docs/compass/current/)

Query operators in MongoDB treat `null` values differently.

This page provides examples of operations that query for `null` values using the [`db.collection.find()`](https://www.mongodb.com/docs/manual/reference/method/db.collection.find.md#mongodb-method-db.collection.find) method in [`mongosh`.](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh)

**Important:**

Use `null` with the MongoDB Shell to query for `null` or missing fields in MongoDB.

The examples on this page use data from the [sample\_mflix dataset](https://www.mongodb.com/docs/manual/sample-data/sample-mflix.md#std-label-sample-mflix). For details on how to load this dataset into your self-managed MongoDB deployment, see [Load the Sample Dataset](https://www.mongodb.com/docs/manual/sample-data/load-sample-data-local.md#std-label-sample-dataset-local). If you made any modifications to the sample databases, you may need to drop and recreate the databases to run the examples on this page.

## Equality Filter

The `{ metacritic : null }` query matches documents that contain the `metacritic` field with a `null` value **or** do not contain the `metacritic` field.

```javascript
db.movies.find( { metacritic: null } )

```

The query returns all documents in the `movies` collection where the `metacritic` field contains a `null` value or does not exist.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ metacritic : { $ne : null } }` query matches documents where the `metacritic` field exists **and** has a non-null value.

```javascript
db.movies.find( { metacritic: { $ne: null } } )

```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ metacritic : { $type: 10 } }` query matches *only* documents that contain the `metacritic` field with a `null` value. The value of the `metacritic` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```javascript
db.movies.find( { metacritic: { $type: 10 } } )

```

The query returns only the documents from the `movies` collection where the `metacritic` field has a `null` value.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ metacritic : { $exists: false } }` query matches documents that do not contain the `metacritic` field:

```javascript
db.movies.find( { metacritic: { $exists: false } } )

```

The query returns only the documents from the `movies` collection that do not contain the `metacritic` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [`pymongo.collection.Collection.find`](https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.find) method in the [PyMongo](https://pymongo.readthedocs.io/en/stable/api) Python driver.

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `None` with the PyMongo Python driver to query for `null` or missing fields in MongoDB.

```python
db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
```

## Equality Filter

The `{ item : None }` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```python
cursor = db.inventory.find({"item": None})
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```python
cursor = db.inventory.find( { "item": { "$ne": None } } )
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item : { $type: 10 } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```python
cursor = db.inventory.find({"item": {"$type": 10}})
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item : { $exists: False } }` query matches documents that do not contain the `item` field:

```python
cursor = db.inventory.find({"item": {"$exists": False}})
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [`motor.motor_asyncio.AsyncIOMotorCollection.find`](https://motor.readthedocs.io/en/stable/api-asyncio/asyncio_motor_collection.html#motor.motor_asyncio.AsyncIOMotorCollection.find) method in the [Motor](https://motor.readthedocs.io/en/stable/) driver.

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `None` with the Motor driver to query for `null` or missing fields in MongoDB.

```python
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
```

## Equality Filter

The `{ item : None }` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```python
cursor = db.inventory.find({"item": None})
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```python
cursor = db.inventory.find( { "item": { "$ne": None } } )
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item : { $type: 10 } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```python
cursor = db.inventory.find({"item": {"$type": 10}})
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item : { $exists: False } }` query matches documents that do not contain the `item` field:

```python
cursor = db.inventory.find({"item": {"$exists": False}})
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [com.mongodb.client.MongoCollection.find](https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/MongoCollection.html#find--) method in the MongoDB [Java Synchronous Driver.](http://mongodb.github.io/mongo-java-driver/3.4/driver/)

**Tip:**

The driver provides [com.mongodb.client.model.Filters](https://mongodb.github.io/mongo-java-driver/3.4/javadoc/com/mongodb/client/model/Filters.html) helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with the Java driver to query for `null` or missing fields in MongoDB.

```java
collection.insertMany(asList(
        Document.parse("{'_id': 1, 'item': null}"),
        Document.parse("{'_id': 2}")
));
```

## Equality Filter

The `eq("item", null)` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```java
FindIterable<Document> findIterable = collection.find(eq("item", null));
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```java
collection.find(ne("item", null));
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `type("item", BsonType.NULL)` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```java
findIterable = collection.find(type("item", BsonType.NULL));
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `exists("item", false)` query matches documents that do not contain the `item` field:

```java
findIterable = collection.find(exists("item", false));
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [com.mongodb.reactivestreams.client.MongoCollection.find](http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/javadoc/com/mongodb/reactivestreams/client/MongoCollection.html#find\(\)) method in the MongoDB [Java Reactive Streams Driver](http://mongodb.github.io/mongo-java-driver-reactivestreams/1.6/).

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with the Java Reactive Streams driver to query for `null` or missing fields in MongoDB.

```java
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
        Document.parse("{'_id': 1, 'item': null}"),
        Document.parse("{'_id': 2}")
));
```

## Equality Filter

The `eq("item", null)` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```java
FindPublisher<Document> findPublisher = collection.find(eq("item", null));
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```java
db.inventory.find( { item: { $ne : null} } )
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `type("item", BsonType.NULL)` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```java
findPublisher = collection.find(type("item", BsonType.NULL));
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The   `exists("item", false)` query matches documents that do not contain the `item` field:

```java
findPublisher = collection.find(exists("item", false));
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values by using the [MongoCollection.find()](https://mongodb.github.io/mongo-java-driver/5.11/apidocs/driver-kotlin-coroutine/mongodb-driver-kotlin-coroutine/com.mongodb.kotlin.client.coroutine/-mongo-collection/find.html) method in the MongoDB [Kotlin Coroutine Driver.](https://www.mongodb.com/docs/drivers/kotlin/coroutine/current/)

**Tip:**

The driver provides [com.mongodb.client.model.Filters](https://mongodb.github.io/mongo-java-driver/5.11/apidocs/driver-core/com/mongodb/client/model/Filters.html) helper methods to facilitate the creation of filter documents. The examples on this page use these methods to create the filter documents.

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with the Kotlin Coroutine driver to query for `null` or missing fields in MongoDB.

```kotlin
collection.insertMany(
    listOf(
        Document("_id", 1)
            .append("item", null),
        Document("_id", 2)
    )
)
```

## Equality Filter

The `eq("item", null)` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```kotlin
val findFlow = collection
    .find(eq("item", null))
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```kotlin
collection.find(ne("item", null))
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `type("item", BsonType.NULL)` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```kotlin
val findFlow = collection
    .find(type("item", BsonType.NULL))
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `exists("item", false)` query matches documents that do not contain the `item` field:

```kotlin
val findFlow = collection
    .find(exists("item", false))
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [Collection.find()](http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#find) method in the [MongoDB Node.js Driver.](http://mongodb.github.io/node-mongodb-native/3.6/)

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with the Node.js driver to query for `null` or missing fields in MongoDB.

```javascript
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
```

## Equality Filter

The `{ item : null }` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```javascript
const cursor = db.collection('inventory').find({
  item: null
});
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```javascript
const cursor = db.collection('inventory')
  .find({ item: { $ne : null }
});
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item : { $type: 10 } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```javascript
const cursor = db.collection('inventory').find({
  item: { $type: 10 }
});
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item : { $exists: false } }` query matches documents that do not contain the `item` field:

```javascript
const cursor = db.collection('inventory').find({
  item: { $exists: false }
});
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [`MongoDB\\Collection::find()`](https://www.mongodb.com/docs/php-library/upcoming/reference/method/MongoDBCollection-find.md#mongodb-phpmethod-phpmethod.MongoDB-Collection--find--) method in the [MongoDB PHP Library](https://www.mongodb.com/docs/drivers/php-libraries/).

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with the MongoDB PHP driver to query for `null` or missing fields in MongoDB.

```php
$insertManyResult = $db->inventory->insertMany([
    ['_id' => 1, 'item' => null],
    ['_id' => 2],
]);
```

## Equality Filter

The `[ item => undef ]` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```php
$cursor = $db->inventory->find(['item' => null]);
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```php
$cursor = $db->inventory->find(['item' => ['$ne' => null ]]);
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `[ item => [ $type => 10 ] ]` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```php
$cursor = $db->inventory->find(['item' => ['$type' => 10]]);
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `[ item => [ $exists => false ] ]` query matches documents that do not contain the `item` field:

```php
$cursor = $db->inventory->find(['item' => ['$exists' => false]]);
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [Mongo::Collection#find()](https://www.mongodb.com/docs/ruby-driver/current/api/Mongo/Collection.html#find-instance_method) method in the [MongoDB Ruby Driver.](https://www.mongodb.com/docs/ruby-driver/current/)

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `nil` with the MongoDB Ruby driver to query for `null` or missing fields in MongoDB.

```ruby
client[:inventory].insert_many([ { _id: 1, item: nil },
                                 { _id: 2 } ])
```

## Equality Filter

The `{ item => nil }` query matches documents that contain the `item` field with a `nil` value **or** do not contain the `item` field.

```ruby
client[:inventory].find(item: nil)
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```ruby
client[:inventory].find(item: { '$ne' => nil })
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item => { $type => 10 } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```ruby
client[:inventory].find(item: { '$type' => 10 })
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item => { $exists => false } }` query matches documents that do not contain the `item` field:

```ruby
client[:inventory].find(item: { '$exists' => false })
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [collection.find()](http://mongodb.github.io/mongo-scala-driver/2.9/scaladoc/org/mongodb/scala/MongoCollection.html#find[C]\(filter:org.mongodb.scala.bson.conversions.Bson\)\(implicite:org.mongodb.scala.bson.DefaultHelper.DefaultsTo[C,TResult],implicitct:scala.reflect.ClassTag[C]\):org.mongodb.scala.FindObservable[C]) method in the [MongoDB Scala Driver](http://mongodb.github.io/mongo-scala-driver/).

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `BsonNull()` with the MongoDB Scala driver to query for `null` or missing fields in MongoDB.

```scala
collection.insertMany(Seq(
  Document("""{"_id": 1, "item": null}"""),
  Document("""{"_id": 2}""")
)).execute()
```

## Equality Filter

The `equal("item", BsonNull)` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```scala
var findObservable = collection.find(equal("item", BsonNull()))
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```scala
collection.find($ne("item", null));
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `type("item", BsonType.NULL)` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```scala
findObservable = collection.find(bsonType("item", BsonType.NULL))
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `exists("item", exists = false)` query matches documents that do not contain the `item` field:

```scala
findObservable = collection.find(exists("item", exists = false))
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [MongoCollection.Find()](https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/M_MongoDB_Driver_MongoCollection_1_Find.htm) method in the [MongoDB .NET/C# Driver](https://mongodb.github.io/mongo-csharp-driver/).

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `BsonNull.Value` with the MongoDB .NET/C# Driver to query for `null` or missing fields in MongoDB.

```csharp
var documents = new[]
{
    new BsonDocument { { "_id", 1 }, { "item", BsonNull.Value } },
    new BsonDocument { { "_id", 2 } }
};
collection.InsertMany(documents);
```

## Equality Filter

The `Eq("item", BsonNull.Value)` query using the [FilterDefinitionBuilder.Eq()](https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Eq.htm) method matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```csharp
var filter = Builders<BsonDocument>.Filter.Eq("item", BsonNull.Value);
var result = collection.Find(filter).ToList();
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```csharp
var filter = Builders<BsonDocument>.Filter.Ne("item", BsonNull.Value);
var result = collection.Find(filter).ToList();
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `Type("item", BsonType.Null)` query using the [FilterDefinitionBuilder.Type()](https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Type.htm) method matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```csharp
var filter = Builders<BsonDocument>.Filter.Type("item", BsonType.Null);
var result = collection.Find(filter).ToList();
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `Exists("item", false)` query using the [FilterDefinitionBuilder.Exists()](https://mongodb.github.io/mongo-csharp-driver/2.10/apidocs/html/Overload_MongoDB_Driver_FilterDefinitionBuilder_1_Exists.htm) method matches documents that do not contain the `item` field:

```csharp
var filter = Builders<BsonDocument>.Filter.Exists("item", false);
var result = collection.Find(filter).ToList();
```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using [mongoc\_collection\_find\_with\_opts.](https://mongoc.org/libmongoc/current/mongoc_collection_find_with_opts.html)

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `BCON_NULL` with the MongoDB C driver to query for `null` or missing fields in MongoDB.

```c
mongoc_collection_t *collection;
mongoc_bulk_operation_t *bulk;
bson_t *doc;
bool r;
bson_error_t error;
bson_t reply;

collection = mongoc_database_get_collection (db, "inventory");
bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
doc = BCON_NEW (
   "_id", BCON_INT64 (1),
   "item", BCON_NULL);

r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
   MONGOC_ERROR ("%s\n", error.message);
   goto done;
}

doc = BCON_NEW ("_id", BCON_INT64 (2));

r = mongoc_bulk_operation_insert_with_opts (bulk, doc, NULL, &error);
bson_destroy (doc);
if (!r) {
   MONGOC_ERROR ("%s\n", error.message);
   goto done;
}

/* "reply" is initialized on success or error */
r = (bool) mongoc_bulk_operation_execute (bulk, &reply, &error);
if (!r) {
   MONGOC_ERROR ("%s\n", error.message);
}
```

## Equality Filter

The `{ item, BCON_NULL }` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

```c
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;

collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW ("item", BCON_NULL);
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```c
filter = BCON_NEW ("item", BCON_NULL);
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item, { $type, BCON_NULL } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```c
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;

collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
   "item", "{",
   "$type", BCON_INT64 (10),
   "}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item, { $exists, BCON_BOOL (false) } }` query matches documents that do not contain the `item` field:

```c
mongoc_collection_t *collection;
bson_t *filter;
mongoc_cursor_t *cursor;

collection = mongoc_database_get_collection (db, "inventory");
filter = BCON_NEW (
   "item", "{",
   "$exists", BCON_BOOL (false),
   "}");
cursor = mongoc_collection_find_with_opts (collection, filter, NULL, NULL);
```

Clean up any open resources by calling the following methods, as appropriate:

- [bson\_destroy](http://mongoc.org/libbson/current/bson_destroy.html)

- [mongoc\_bulk\_operation\_destroy](https://mongoc.org/libmongoc/current/mongoc_bulk_operation_destroy.html)

- [mongoc\_collection\_destroy](https://mongoc.org/libmongoc/current/mongoc_collection_destroy)

- [mongoc\_cursor\_destroy,](https://mongoc.org/libmongoc/current/mongoc_cursor_destroy.html)

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using the [Collection.Find](https://godoc.org/github.com/mongodb/mongo-go-driver/mongo#Collection.Find) function in the [MongoDB Go Driver](https://github.com/mongodb/mongo-go-driver/).

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `nil` with the MongoDB Go driver to query for `null` or missing fields in MongoDB.

```go

docs := []any{
	bson.D{
		{"_id", 1},
		{"item", nil},
	},
	bson.D{
		{"_id", 2},
	},
}

result, err := coll.InsertMany(context.TODO(), docs)

```

## Equality Filter

The `item => nil` query matches documents that contain the `item` field with a `nil` value **or** do not contain the `item` field.

```go

cursor, err := coll.Find(
	context.TODO(),
	bson.D{
		{"item", nil},
	})

```

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```go
cursor, err := coll.Find(
  context.TODO(),
  bson.D{
    {"item", bson.D{"$ne": nil}},
  })
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The following query matches *only* documents that contain the `item` field with a value of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

```go

cursor, err := coll.Find(
	context.TODO(),
	bson.D{
		{"item", bson.D{
			{"$type", 10},
		}},
	})

```

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `bson.D{{"item", bson.D{{"$exists", false}}}}` query matches documents that do not contain the `item` field:

```go

cursor, err := coll.Find(
	context.TODO(),
	bson.D{
		{"item", bson.D{
			{"$exists", false},
		}},
	})

```

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

This page provides examples of operations that query for `null` values using [MongoDB Compass.](https://www.mongodb.com/docs/compass/current.md#std-label-compass-index)

The examples on this page use the `inventory` collection. Connect to a test database in your MongoDB instance then create the `inventory` collection:

**Important:**

Use `null` with MongoDB Compass to query for `null` or missing fields in MongoDB.

```javascript
[
   { "_id": 1, "item": null },
   { "_id": 2 }
]
```

For instructions on inserting documents in MongoDB Compass, see [Insert Documents.](https://www.mongodb.com/docs/manual/tutorial/insert-documents.md#std-label-write-op-insert)

## Equality Filter

The `{ item : null }` query matches documents that contain the `item` field with a `null` value **or** do not contain the `item` field.

Copy the following query filter document into the [query bar](https://www.mongodb.com/docs/compass/current/query/filter.md#std-label-compass-query-bar) and click Find:

```javascript
{ item: null }
```

![Query null value or missing field](/images/compass-find-null-field.png)

The query returns both documents in the collection.

**Note: Dotted Paths That Traverse Arrays**

A dotted path can produce results you might not expect when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

## Non-Equality Filter

To query for fields that **exist** and are **not null**, use the `{ $ne : null }` filter.

**Note: Dotted Paths That Traverse Arrays**

The `{ $ne : null }` filter on a dotted path can match a document even when `{ path : { $exists: true } }` does not, when the path traverses an array. For details, see [Null Comparisons on Array Fields.](https://www.mongodb.com/docs/manual/tutorial/query-for-null-fields.md#std-label-null-semantics-arrays)

The `{ item : { $ne : null } }` query matches documents where the `item` field exists **and** has a non-null value.

```javascript
{ item: { $ne : null } }
```

## Null Comparisons on Array Fields

When a field holds an array, MongoDB compares the query value against each element of the array and against the array itself. As a result, comparisons to `null` on array fields produce results that you might not expect. The examples in this section use [`mongosh`](https://www.mongodb.com/docs/mongodb-shell.md#mongodb-binary-bin.mongosh) syntax.

Consider a collection that contains the following documents:

```javascript
{ _id: 1, a: null }
{ _id: 2, a: [ ] }
{ _id: 3, a: [ 1, "string", 4 ] }
{ _id: 4, a: [ 1, null, 4 ] }
{ _id: 5, a: [ { b: 1 }, { b: 3 } ] }
{ _id: 6, a: [ { b: 3 }, { } ] }
{ _id: 7 }
```

### Equality on an Array Field

The `{ a: null }` query matches a document when the `a` field is `null`, when the `a` field is missing, or when the `a` array contains at least one `null` element. The query matches the documents with `_id` values `1`, `4`, and `7`.

An empty array and an array without a `null` element do not match. The `a` field exists in both cases, and neither array contains a `null` element, so the documents with `_id` values `2` and `3` are not returned.

The `{ a: { $ne: null } }` query returns the complement, which is every document that `{ a: null }` does not match. The query matches the documents with `_id` values `2`, `3`, `5`, and `6`, including the document that holds an empty array.

### Equality on a Dotted Path

The `{ "a.b": null }` query matches a document in any of these cases:

- The `a` field is missing, or holds a value that is neither an object nor an array.

- The `a` field holds an object where `b` is missing or `null`.

- The `a` field holds an array with at least one object where `b` is missing or `null`.

Against the sample documents, the query matches the `_id` values `1`, `6`, and `7`. The document with `_id: 6` matches because one of its array elements, `{ }`, is missing `b`, even though the other element has a non-null `b` value.

An empty array, an array of scalar values, and an array that contains a literal `null` do not match, because none of these array shapes contains an object for `b` to be missing from. The documents with `_id` values `2`, `3`, and `4` do not match for this reason. The document with `_id: 5` does not match either, because every object in its array has a non-null `b` value.

The `{ "a.b": { $ne: null } }` query returns the complement, which is every document that `{ "a.b": null }` does not match: the documents with `_id` values `2`, `3`, `4`, and `5`. That result is not the same as the set of documents that have a non-null value for `a.b`. Neither `_id: 2` nor `_id: 3` has any value at `a.b`, yet the `{ $ne: null }` filter still matches them. A `{ $ne: null }` filter on a dotted path does not necessarily return the same documents as an [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) check on that path.

## Type Check

The `{ item : { $type: 10 } }` query matches *only* documents that contain the `item` field with a `null` value. The value of the `item` field is of [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

Copy the following query filter document into the [query bar](https://www.mongodb.com/docs/compass/current/query/filter.md#std-label-compass-query-bar) and click Find:

```javascript
{ item : { $type: 10 } }
```

![Find null type](/images/compass-find-null-type-check.png)

The query returns only the document where the `item` field has a value of `null`.

## Existence Check

The following example queries for documents that do not contain a field.

The `{ item : { $exists: false } }` query matches documents that do not contain the `item` field:

Copy the following query filter document into the [query bar](https://www.mongodb.com/docs/compass/current/query/filter.md#std-label-compass-query-bar) and click Find:

```javascript
{ item : { $exists: false } }
```

![Query for null value](/images/compass-find-null-existence-check.png)

The query only returns the document that does *not* contain the `item` field.

**Note:**

[Expressions](https://www.mongodb.com/docs/manual/reference/mql/expressions.md#std-label-aggregation-expressions) do not support the [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operator. To check for the existence of a field in an expression, you can use the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#mongodb-expression-exp.-type) aggregation operator to check if a field has a type of `missing`.

For more information, see [$type Existence Check.](https://www.mongodb.com/docs/manual/reference/operator/aggregation/type.md#std-label-missing-type-existence-check)

**See also:**

Reference documentation for the [`$type`](https://www.mongodb.com/docs/manual/reference/operator/query/type.md#mongodb-query-op.-type) and [`$exists`](https://www.mongodb.com/docs/manual/reference/operator/query/exists.md#mongodb-query-op.-exists) operators.

## Query for Null or Missing Fields with MongoDB Atlas

The example in this section uses the [sample training dataset](https://www.mongodb.com/docs/atlas/sample-data/sample-training/). To learn how to load the sample dataset into your MongoDB Atlas deployment, see [Load Sample Data.](https://www.mongodb.com/docs/atlas/sample-data/#std-label-load-sample-data)

To query for a `null` or missing field in MongoDB Atlas, follow these steps:

1. In the MongoDB Atlas UI, go to the Clusters page for your project.

   If it's not already displayed, select the organization that contains your desired project from the  Organizations menu in the navigation bar.

   If it's not already displayed, select your project from the Projects menu in the navigation bar.

   In the sidebar, click Clusters under the Database heading.

   The [Clusters](https://cloud.mongodb.com/go?l=https%3A%2F%2Fcloud.mongodb.com%2Fv2%2F%3Cproject%3E%23%2Fclusters) page displays.

2. Navigate to the collection.

   For the cluster that contains the sample data, click Browse Collections.

   In the left navigation pane, select the `sample_training` database.

   Select the `companies` collection.

3. Insert a blank document.

   Click the Insert Document button to display the dialog box, and then click Insert to insert a document with only the `_id` field.

4. Specify a query filter document.

   To find a document that contains a `null` or missing value, specify a [query filter document](https://www.mongodb.com/docs/manual/core/documents-other-uses.md#std-label-document-query-filter) in the Filter field. A query filter document uses [query operators](https://www.mongodb.com/docs/manual/core/csfle/reference/supported-operations.md#std-label-csfle-supported-query-operators) to specify search conditions.

   Query operators in MongoDB treat `null` values differently. To apply a query filter, copy each of the following documents into the Filter search bar and click Apply.

   Use the following query filter to match documents that contain a `description` field with a `null` value or do not contain the `description` field:

   ```javascript
   { description : null }
   ```

   Use the following query filter to match only documents that contain a `description` field with a `null` value. This filter specifies that the value of the field must be [BSON Type](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-bson-types) `Null` (BSON Type 10):

   ```javascript
   { description : { $type: 10 } }
   ```

   Use the following query filter to match only documents that do not contain the `description` field. Only the document that you inserted earlier should appear:

   ```javascript
   { description : { $exists: false } }
   ```
