Navigation

exists (Operator)

On this page

Definition

exists

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 query in conjunction with other search clauses.

exists has the following syntax:

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

Examples

The following examples 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
  }
}

The fruit collection has a Full Text Search index on the description field that uses the standard analyzer. The standard analyzer lower-cases all words and disregards common stop words ("the", "a", "and", etc).

Basic Example

The following example searches for documents which include a field called type:

db.fruit.aggregate([
  {
    $searchBeta: {
      "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.

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([
  {
    $searchBeta: {
      "exists": {
        "path": "quantities.lemons"
      }
    }
  }
])

The above query returns the following result:

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

Compound Example

The following example uses exists as part of a compound query.

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

The above query returns the following results:

{
  "_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 clause.