in
For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt
<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset
Available tabs: other tabs: single, multiple, compound -->
in Operator
Definition
The in operator in MongoDB Search performs a search for an array of BSON (Binary Javascript Object Notation) 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 |
|---|---|---|---|
path |
string or array of strings | Indexed field or fields to search. You can also specify a wildcard path to search. To search for [string](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-string values in a field, you must index the field as the MongoDB Search [token](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-bson-data-types-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 |
[boolean](https://www.mongodb.com/docs/search/indexes/field-types/boolean-type.md#std-label-boolean-ref, [objectId](https://www.mongodb.com/docs/search/indexes/field-types/object-id-type.md#std-label-objectId-ref, [number](https://www.mongodb.com/docs/search/indexes/field-types/number-type.md#std-label-bson-data-types-number, [date](https://www.mongodb.com/docs/search/indexes/field-types/date-type.md#std-label-bson-data-types-date, [uuid](https://www.mongodb.com/docs/search/indexes/field-types/uuid-type.md#std-label-bson-data-types-uuid, or [string](https://www.mongodb.com/docs/search/indexes/field-types/string-type.md#std-label-bson-data-types-string | Value or values to search. Value can be either a single value or an array of values of only one of the supported BSON (Binary Javascript Object Notation) types and can't be a mix of different types. To search for [string](https://www.mongodb.com/docs/manual/reference/bson-types.md#std-label-document-bson-type-string values in a field, you must index the field as the MongoDB Search [token](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-bson-data-types-token type. | Required |
doesNotAffect |
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 $search.facets or $searchMeta.facets. The operator uses the specified facets for filtering documents to match this query, but doesn't recalculate counts for them. For more information, see [Multi-Select Facets.](https://www.mongodb.com/docs/search/query/operators-collectors/facet.md#std-label-fts-facet-multi-select |
Optional |
Examples
The following examples use the in operator to query collections in the [sample_analytics.customers](https://www.mongodb.com/docs/manual/sample-data/sample-analytics.md#std-label-sample-analytics collection. If you load the sample data on your cluster and [create](https://www.mongodb.com/docs/search/indexes/manage-indexes.md#std-label-ref-create-index a MongoDB Search index named default that uses [static mappings](https://www.mongodb.com/docs/search/indexes/define-field-mappings.md#std-label-static-dynamic-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](https://www.mongodb.com/docs/search/indexes/field-types/token-type.md#std-label-bson-data-types-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.](https://www.mongodb.com/docs/search/indexes/manage-indexes.md#std-label-ref-create-index
Sample Queries
Single Value Field Match
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](https://www.mongodb.com/docs/manual/reference/operator/aggregation/project.md#mongodb-pipeline-pipe.-project stage to:
-
Omit the
_idfield in the results. -
Include only the
nameandbirthdatefields in the results.
db.customers.aggregate([
{
"$search": {
"in": {
"path": "birthdate",
"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")]
}
}
},
{
"$project": {
"_id": 0,
"name": 1,
"birthdate": 1
}
}
])
Output:
[
{
name: 'Elizabeth Ray',
birthdate: ISODate("1977-03-02T02:20:31.000Z")
},
{
name: 'Brad Cardenas',
birthdate: ISODate("1977-05-06T21:57:35.000Z")
}
]
MongoDB Search returns two documents that it matches with the dates specified in the query.
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
db.customers.aggregate([
{
"$searchMeta": {
"facet": {
"operator": {
"in": {
"path": "active",
"value": null
}
},
"facets": {
"birthdateFacet": {
"type": "date",
"path": "birthdate",
"boundaries": [ISODate("1970-01-01"), ISODate("1980-01-01"), ISODate("1990-01-01"), ISODate("2000-01-01")],
"default": "other"
}
}
}
}
}
])
Output:
[
{
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')
}
]
}
}
}
]
