Definition
The in operator in MongoDB Search 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.
Syntax
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>, "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] } } }
Fields
Field | Type | Description | Necessity |
|---|---|---|---|
| string or array of strings | Required | |
| object | Score to assign to matching search term results. Use one of the following options to modify the score:
| Optional |
| Required | ||
| string or array of strings | Facet or list of facets to exclude from count recalculation on the basis of this query. Value must be one or more names of a facet defined in | Optional |
Examples
The following examples use the in operator to query collections in the sample_analytics.customers collection. If you load the sample data on your cluster and create a MongoDB Search index named default that uses static mappings on the collection, you can run the following queries against the collections.
Sample Index
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
namefield 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 a MongoDB Search index, see Manage MongoDB Search Indexes.
Sample Queries
The following query uses the in operator to search the birthdate field, which contains a single value, for customers who were born on given dates. The query uses the $project stage to:
Omit the
_idfield in the results.Include only the
nameandbirthdatefields in the results.
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "in": { 5 "path": "birthdate", 6 "value": [ISODate("1977-03-02T02:20:31.000+00:00"), ISODate("1977-03-01T00:00:00.000+00:00"), ISODate("1977-05-06T21:57:35.000+00:00")] 7 } 8 } 9 }, 10 { 11 "$project": { 12 "_id": 0, 13 "name": 1, 14 "birthdate": 1 15 } 16 } 17 ])
1 [ 2 { 3 name: 'Elizabeth Ray', 4 birthdate: ISODate("1977-03-02T02:20:31.000Z") 5 }, 6 { 7 name: 'Brad Cardenas', 8 birthdate: ISODate("1977-05-06T21:57:35.000Z") 9 } 10 ]
MongoDB Search returns two documents that it matches with the dates specified in the query.
The following query uses the in operator to query the accounts field, which contains an array of numbers, for customers with account numbers 371138, 371139, or 371140. The query uses the $project stage to:
Omit the
_idfield in the results.Include only the
nameandaccountsfields in the results.
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "in": { 5 "path": "accounts", 6 "value": [371138, 371139, 371140] 7 } 8 } 9 }, 10 { 11 "$project": { 12 "_id": 0, 13 "name": 1, 14 "accounts": 1 15 } 16 } 17 ])
1 [ 2 { 3 name: 'Elizabeth Ray', 4 accounts: [ 371138, 324287, 276528, 332179, 422649, 387979 ] 5 } 6 ]
MongoDB Search returns only one document that it matches with the account number 371138 specified in the query.
The following query uses the text operator to query for customers whose first name is James in the name field. The query specifies preference using the in operator for customers associated with the given objectIds in the _id field. The query uses $limit stage to limit the output to 5 results and the $project stage to:
Include only the
_idandnamefields in the results.Add a field named
scoreto the results.
1 db.customers.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "in": { 7 "path": "name", 8 "value": ["james sanchez", "jennifer lawrence"] 9 } 10 }], 11 "should": [{ 12 "in": { 13 "path": "_id", 14 "value": [ObjectId("5ca4bbcea2dd94ee58162a72"), ObjectId("5ca4bbcea2dd94ee58162a91")] 15 } 16 }] 17 } 18 } 19 }, 20 { 21 "$limit": 5 22 }, 23 { 24 "$project": { 25 "_id": 1, 26 "name": 1, 27 "score": { $meta: "searchScore" } 28 } 29 } 30 ])
1 [ 2 { 3 _id: ObjectId("5ca4bbcea2dd94ee58162a72"), 4 name: 'James Sanchez', 5 score: 2 6 }, 7 { 8 _id: ObjectId("5ca4bbcea2dd94ee58162a71"), 9 name: 'Jennifer Lawrence', 10 score: 1 11 } 12 ]
MongoDB Search returns documents that contain James Sanchez and Jennifer Lawrence in the name field. MongoDB Search scores the document that contains name: 'James Sanchez' higher because it matches the ObjectId specified in the should clause.
Facet Query
The following query uses the in operator to search the active field, which contains a boolean value, for customers who are active. The query returns the number of active customers whose birthdate falls in the following buckets:
1970-01-01, inclusive lower bound for this bucket
1980-01-01, exclusive upper bound for the 1970-01-01 bucket and inclusive lower bound for this bucket
1990-01-01, exclusive upper bound for the 1980-01-01 bucket and inclusive lower bound for this bucket
2000-01-01, exclusive upper bound for the 1990-01-01 bucket
1 db.customers.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "in": { 7 "path": "active", 8 "value": null 9 } 10 }, 11 "facets": { 12 "birthdateFacet": { 13 "type": "date", 14 "path": "birthdate", 15 "boundaries": [ISODate("1970-01-01"), ISODate("1980-01-01"), ISODate("1990-01-01"), ISODate("2000-01-01")], 16 "default": "other" 17 } 18 } 19 } 20 } 21 } 22 ])
[ { count: { lowerBound: Long('1') }, facet: { birthdateFacet: { buckets: [ { _id: ISODate('1970-01-01T00:00:00.000Z'), count: Long('1') }, { _id: ISODate('1980-01-01T00:00:00.000Z'), count: Long('0') }, { _id: ISODate('1990-01-01T00:00:00.000Z'), count: Long('0') } ] } } } ]