Navigation

range

Definition

range

The range operator supports querying and scoring numeric and date values. This operator can be used to perform a search over:

  • Number fields of BSON int32, int64, and double data types.
  • Date fields of BSON date data type in ISODate format.

You can use the range operator to find results that are within a given numeric or date range.

Syntax

range has the following syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
    "$search": {
       "index": <index name>, // optional, defaults to "default"
       "range": {
          "path": "<field-to-search>",
          "gt | gte": <value-to-search>,
          "lt | lte": <value-to-search>,
          "score": <score-options>
       }
    }
}

Options

range uses the following terms to construct a query:

Field Type Description Necessity
gt or gte BSON date or number

Find values greater than (>) or greater than or equal to (>=) the given value.

  • For number fields, the value can be an int32, int64, or double data type.
  • For date fields, the value must be an ISODate formatted date.
no
lt or lte BSON date or number

Find values less than (<) or less than or equal to (<=) the given value.

  • For number fields, the value can be an int32, int64, or double data type.
  • For date fields, the value must be an ISODate formatted date.
no
path string or array of strings Indexed field or fields to search. See Path Construction. yes
score object

Modify the score assigned to matching search results. Options are:

  • boost: multiply the result score by the given number.
  • constant: replace the result score with the given number.

For information on using score in your query, see Scoring.

no

Limitation

You cannot use the range operator to query values stored in an array. Atlas Search cannot index numeric or date values if they are part of an array.

Examples

The following examples use the movies collection in the sample_mflix database. If you loaded the sample dataset on your cluster, you can create the static indexes using the index definitions in the examples below and run the queries on your cluster. The Tutorial: Create and Query an Atlas Search Index contains instructions for loading the sample dataset, creating an index definition, and running Atlas Search queries.

Number Example

The following examples uses the range operator to query a number field.

Example 1

The following example uses gte and lte fields to define the numeric range to search.

Example

The following index definition named runtimes indexes the runtime field in the movies collection.

{
   "mappings": {
      "dynamic": false,
      "fields": {
         "runtime": {
            "type": "number"
         }
      }
   }
}

The following query searches for movies with a runtime greater than or equal to 2 and less than equal to 3. It includes a $limit stage to limit the output to 5 results and a $project stage to exclude all fields except title and runtime.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
db.movies.aggregate([
   {
      $search: {
         "index": "runtimes",
         "range": {
            "path": "runtime",
            "gte": 2,
            "lte": 3
         }
      }
   },
   {
      $limit: 5
   },
   {
      $project: {
         "_id": 0,
         "title": 1,
         "runtime": 1
      }
   }
])

The above query returns the following search results:

{ "runtime" : 3, "title" : "Dots" }
{ "runtime" : 3, "title" : "Sisyphus" }
{ "runtime" : 3, "title" : "The Fly" }
{ "runtime" : 2, "title" : "Andrè and Wally B." }
{ "runtime" : 2, "title" : "Luxo Jr." }

Example 2

The following example uses the lte field to search for all values less than or equal to the given value.

Example

The following index definition named runtimes indexes the runtime field in the movies collection.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
{
   "mappings": {
      "dynamic": false,
      "fields": {
         "runtime": {
            "type": "number"
         }
      }
   }
}

The following query searches for all movies with a runtime less than or equal to 2. It includes a $limit stage to limit the output to 5 results and a $project stage to:

  • Exclude all fields except title and runtime
  • Add a field named score
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
db.movies.aggregate([
   {
      $search: {
         "index": "runtimes",
         "range": {
            "path": "runtime",
            "lte": 2
         }
      }
   },
   {
      $limit: 5
   },
   {
      $project: {
         "_id": 0,
         "title": 1,
         "runtime": 1,
         score: { $meta: "searchScore" }
      }
   }
])

The above query returns the following search results:

{ "runtime" : 1, "title" : "Blacksmith Scene", "score" : 1 }
{ "runtime" : 2, "title" : "Andrè and Wally B.", "score" : 1 }
{ "runtime" : 2, "title" : "Luxo Jr.", "score" : 1 }
{ "runtime" : 1, "title" : "The Kiss", "score" : 1 }
{ "runtime" : 1, "title" : "Dickson Experimental Sound Film", "score" : 1 }

Date Example

The following example uses the range operator to query a date field.

Example

The following index definition named releaseddate indexes the released field in the movies collection:

{
   "mappings": {
      "dynamic": false,
      "fields": {
         "released": {
            "type": "date"
         }
      }
   }
}

The following query searches for movies released between January 1, 2010 and January 1, 2015. It includes a $limit stage to limit the output to 5 results and a $project stage to exclude all fields except title and released.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
db.movies.aggregate([
   {
      $search: {
         "index": "releaseddate",
         "range": {
            "path": "released",
            "gt": ISODate("2010-01-01T00:00:00.000Z"),
            "lt": ISODate("2015-01-01T00:00:00.000Z")
         }
      }
   },
   {
      $limit: 5
   },
   {
      $project: {
         "_id": 0,
         "title": 1,
         "released": 1
      }
   }
])

The above query returns the following search results:

1
2
3
4
5
{ "title" : "The Fall of the House of Usher", "released" : ISODate("2011-09-20T00:00:00Z") }
{ "title" : "The Blood of a Poet", "released" : ISODate("2010-05-20T00:00:00Z") }
{ "title" : "Too Much Johnson", "released" : ISODate("2014-08-30T00:00:00Z") }
{ "title" : "Stolen Desire", "released" : ISODate("2012-07-01T00:00:00Z") }
{ "title" : "The Monkey King", "released" : ISODate("2012-01-12T00:00:00Z") }
←   queryString regex  →