Docs Menu
Docs Home
/
MongoDB 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 Construct a Query Path for more information.

To search for 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.

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.

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 _id field in the results.

  • Include only the name and birthdate fields in the results.

1db.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]

Atlas 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 _id field in the results.

  • Include only the name and accounts fields in the results.

1db.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]

Atlas 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 _id and name fields in the results.

  • Add a field named score to the results.

1db.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]

Atlas Search returns documents that contain James Sanchez and Jennifer Lawrence in the name field. Atlas Search scores the document that contains name: 'James Sanchez' higher because it matches the ObjectId specified in the should clause.

Back

geoWithin