Docs Menu
Docs Home
/
MongoDB Atlas
/ / / /

exists

On this page

  • Definition
  • Syntax
  • Options
  • Scoring Behavior
  • Examples
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:

1{
2 $search: {
3 "index": <index name>, // optional, defaults to "default"
4 "exists": {
5 "path": "<field-to-test-for>",
6 "score": <options>
7 }
8 }
9}

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.
no

Atlas Search assigns a constant score of 1 for all the documents in the result set. You can customize the default Atlas Search score using the score options. To learn more about modifying the default score returned by Atlas Search, see Modify the Score.

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

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

1{
2 "_id" : 1,
3 "type" : "apple",
4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp."
5},
6{
7 "_id" : 2,
8 "type" : "banana",
9 "description" : "Bananas are usually sold in bunches of five or six."
10},
11{ "_id" : 3,
12 "type": "apple",
13 "description" : "Apple pie and apple cobbler are popular apple-based desserts."
14},
15{ "_id" : 4,
16 "description" : "Types of citrus fruit include lemons, oranges, and grapefruit.",
17 "quantities" : {
18 "lemons": 200,
19 "oranges": 240,
20 "grapefruit": 160
21 }
22}

The fruit collection has a default dynamic Atlas Search index that uses the default standard analyzer. The standard analyzer lower-cases all words and disregards common stop words ("the", "a", "and", etc).

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

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

1db.fruit.aggregate([
2 {
3 $search: {
4 "exists": {
5 "path": "type"
6 }
7 }
8 }
9])

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 Atlas Search Playground.

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.

1db.fruit.aggregate([
2 {
3 "$search": {
4 "exists": {
5 "path": "quantities.lemons"
6 }
7 }
8 }
9])

Try this in the Atlas Search Playground.

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

1db.fruit.aggregate([
2 {
3 $search: {
4 "compound": {
5 "must": [
6 {
7 "exists": {
8 "path": "type"
9 }
10 },
11 {
12 "text": {
13 "query": "apple",
14 "path": "type"
15 }
16 }],
17 "should": {
18 "text": {
19 "query": "fuji",
20 "path": "description"
21 }
22 }
23 }
24 }
25 }
26])

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.

Try this in the Atlas Search Playground.

Back

equals

Next

facet