exists (MongoDB Search Operator)

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

exists (MongoDB Search Operator)

Definition

The exists operator tests if a path to a specified indexed field name exists in a document. If the specified field exists but is not indexed, the document is not included with the result set. exists is often used as part of a [compound](https://www.mongodb.com/docs/search/query/operators-collectors/compound.md#std-label-compound-ref query in conjunction with other search clauses.

Syntax

exists has the following syntax:

{
  $search: {
    "index": <index name>, // optional, defaults to "default"
    "exists": {
      "path": "<field-to-test-for>",
      "score": <options>
    }
  }
}

Options

exists uses the following terms to construct a query:

Field Type Description Required?
path string Indexed field to search. yes
score object Score to assign to matching search results. To learn more about the options to modify the default score, see [Score the Documents in the Results.](https://www.mongodb.com/docs/search/query/score/overview.md#std-label-scoring-ref no

Scoring Behavior

MongoDB Search assigns a [constant](https://www.mongodb.com/docs/search/query/score/modify-score.md#std-label-scoring-constant score of 1 for all the documents in the result set. You can customize the default MongoDB Search score using the score options. To learn more about modifying the default score returned by MongoDB Search, see [Modify the Score.](https://www.mongodb.com/docs/search/query/score/modify-score.md#std-label-scoring-options

Examples

You can try the following examples in the MongoDB Search Playground or your cluster.

Sample Collection

The examples on this page use a collection called fruit that contains the following documents:

{
  "_id" : 1,
  "type" : "apple",
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
},
{
  "_id" : 2,
  "type" : "banana",
  "description" : "Bananas are usually sold in bunches of five or six."
},
{ "_id" : 3,
  "type": "apple",
  "description" : "Apple pie and apple cobbler are popular apple-based desserts."
},
{ "_id" : 4,
  "description" : "Types of citrus fruit include lemons, oranges, and grapefruit.",
  "quantities" : {
    "lemons": 200,
    "oranges": 240,
    "grapefruit": 160
  }
}

Sample Index

The fruit collection has a default [dynamic](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-mappings MongoDB Search index that uses the default [standard](https://www.mongodb.com/docs/search/indexes/analyzers/standard.md#std-label-ref-standard-analyzer analyzer. The standard analyzer lower-cases all words and disregards common stop words (“the”, “a”, “and”, etc).

Sample Queries

The following queries demonstrate the exists operator in MongoDB Search queries.

Basic Example

The following example searches for documents which include a field named type.

db.fruit.aggregate([
  {
    $search: {
      "exists": {
        "path": "type"
      }
    }
  }
])

The above query returns the first three documents of the collection. The document with _id: 4 is not included because it does not have a type field.

➤ Try this in the MongoDB Search Playground.

Embedded Example

Use dot notation to search for embedded fields. The following example searches for documents which have a field named lemons embedded within a field named quantities.

db.fruit.aggregate([
  {
    "$search": {
      "exists": {
        "path": "quantities.lemons"
      }
    }
  }
])

Output:

{
  "_id" : 4,
  "description" : "Types of citrus fruit include lemons, oranges, and grapefruit.",
  "quantities" : {
    "lemons": 200,
    "oranges": 240,
    "grapefruit": 160
  }
}

➤ Try this in the MongoDB Search Playground.

Compound Example

The following example uses exists as part of a [compound](https://www.mongodb.com/docs/search/query/operators-collectors/compound.md#std-label-compound-ref query.

db.fruit.aggregate([
  {
    $search: {
      "compound": {
        "must": [
          {
            "exists": {
              "path": "type"
            }
          },
          {
            "text": {
              "query": "apple",
              "path": "type"
            }
          }],
        "should": {
          "text": {
            "query": "fuji",
            "path": "description"
          }
        }
      }
    }
  }
])

Output:

{
  "_id" : 1,
  "type" : "apple",
  "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
}
{
  "_id" : 3,
  "type" : "apple",
  "description" : "Apple pie and apple cobbler are popular apple-based desserts."
}

Both documents have a type field, and both include the search term apple. The document with _id: 1 is returned first because it satisfies the [should](https://www.mongodb.com/docs/search/query/operators-collectors/compound.md#mongodb-data-should clause.

➤ Try this in the MongoDB Search Playground.