Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

in

On this page

  • Definition
  • Syntax
  • Fields
  • Examples
  • Sample Index
  • Sample Queries

The in operator performs a search for an array of BSON number, date, boolean, objectId, uuid, or string values at the given path and returns documents where the value of the field equals any value in the specified array. If the field holds an array, then the in operator selects the documents whose field holds an array that contains at least one element that matches any value in the specified array.

The in operator has the following syntax:

{
$search: {
"index": <index name>, // optional, defaults to "default"
"in": {
"path": "<field-to-search>",
"score": <options>,
"value": <single-or-array-of-values-to-search>
}
}
}
Field
Type
Description
Necessity
path
string

Indexed field to search. You can also specify a wildcard path to search. See path construction for more information.

Note

To search string values in a field, you must index the field as the Atlas Search token type.

Required
score
object

Score to assign to matching search term results. Use one of the following options to modify the score:

  • boost: multiply the result score by the given number.

  • constant: replace the result score with the given number.

  • function: replace the result score using the function expression.

Optional
value

Value or values to search. Value can be either a single value or an array of values of only one of the supported BSON types and can't be a mix of different types.

Note

To search for string values in a field, you must index the field as the Atlas Search token type.

Required

The following examples use the in operator to query collections in the sample_analytics.customers collection. If you load the sample data on your Atlas cluster and create an Atlas Search index named default that uses static mappings on the collection, you can run the following queries against the collections.

The sample index definition specifies the following actions to support in operator queries against the indexed fields in the collection:

  • Automatically index all the dynamically indexable fields in the collection.

  • Statically index the name field as the token type and converts the text in the field to lowercase.

{
"mappings": {
"index": "default",
"dynamic": true,
"fields": {
"name": {
"normalizer": "lowercase",
"type": "token"
}
}
}
}

To learn how to create an Atlas Search index, see Create an Atlas Search Index.

←  geoWithinknnBeta →