定義
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.
構文
in演算子の構文は次のとおりです。
{ $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>] } } }
フィールド
フィールド | タイプ | 説明 | 必要性 |
|---|---|---|---|
| 文字列または複数の文字列の配列 | 必須 | |
| オブジェクト | 一致する検索タームの結果に割り当てる スコア 。 スコアを変更するには、次のいずれかのオプションを使用します。
| 任意 |
| 必須 | ||
| 文字列または複数の文字列の配列 | このクエリに基づいてカウントの再計算から除外するファセットまたはファセットのリスト。値は、 | 任意 |
例
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.
サンプル インデックス
サンプルのインデックス定義では、コレクション内のインデックス付きフィールドに対するin演算子クエリをサポートするために、次のアクションを指定します。
コレクション内のすべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。
nameフィールドをトークンタイプとして静的にインデックスし、フィールドのテキストを小文字に変換します。
{ "mappings": { "index": "default", "dynamic": true, "fields": { "name": { "normalizer": "lowercase", "type": "token" } } } }
MongoDB Searchインデックスの作成方法については、 MongoDB Search インデックスの管理 を参照してください。
サンプル クエリ
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:
結果の
_idフィールドを省略します。namebirthdate結果には フィールドと フィールドのみが含まれます。
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 は、クエリで指定された日付に一致する 2 つのドキュメントを返します。
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:
結果の
_idフィールドを省略します。nameaccounts結果には フィールドと フィールドのみが含まれます。
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 では、クエリで指定されたアカウント番号 371138 に一致するドキュメントが 1 つだけ返されます。
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:
_idname結果には フィールドと フィールドのみが含まれます。scoreという名前のフィールドを結果に追加する。
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 では、nameフィールドに James Sanchez と Jennifer Lawrence を含むドキュメントが返されます。MongoDB Search では、name: 'James Sanchez' を含むドキュメントは、should 句で指定された ObjectId と一致するため、スコアが高くなります。
ファセット クエリ
次のクエリでは、in 演算子を使用して、ブール値を含む active フィールドでアクティブなカスタマーを検索します。このクエリは、生年月日が次のバケットに該当するアクティブなカスタマーの数を返します。
1970-01-01、このバケットの下限値を含む
1980-01-01、1970-01-01バケットの上限(排他的)とこのバケットの下限(包括的)
1990-01-01、1980-01-01バケットの上限(排他的)とこのバケットの下限(包括的)
2000-01-01、1990-01-01バケットの排他的上限
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') } ] } } } ]