定義
equalsequals演算子は、フィールドが指定された値と一致するかどうかを確認します。equalsは次のデータ型のクエリをサポートしています。数値(
int32、int64、doubleなど)null
equals演算子を使用して、配列内のブール値、ObjectId、数値、日付、文字列(token型としてインデックス付き)をクエリできます。配列内の少なくとも 1 つの要素がequals演算子の「値」フィールドと一致する場合、 MongoDB Search はそのドキュメントを結果セットに追加します。注意
equals演算子は 15 桁の 10 進数までの数値をサポートします。 ドキュメントまたはクエリに小数点が追加されると、精度の問題やクエリの不正確性が生じる可能性があります。
構文
equals の構文は次のとおりです。
{ $search: { "index": <index name>, // optional, defaults to "default" "equals": { "path": "<field-to-search>", "value": <boolean-value>|<objectId>|<number>|<date>|<string>, "score": <score-options>, "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] } } }
オプション
equals では、次の用語を使用してクエリを作成します。
フィールド | タイプ | 説明 | 必須 |
|---|---|---|---|
| string | 検索するインデックス付きフィールド。 | はい |
| クエリする値。 | はい | |
| オブジェクト | 一致する検索タームの結果に割り当てる スコア 。 スコアを変更するには、次のいずれかのオプションを使用します。
クエリで | no |
| 文字列または複数の文字列の配列 | このクエリに基づいてカウントの再計算から除外するファセットまたはファセットのリスト。値は、 | no |
スコアリングの動作
デフォルトでは、 equalsはconstantスコアリングを使用します。 一致する各ドキュメントには1のスコアが与えられます。
配列値をクエリする場合、配列内のクエリに一致する値が多いほど、 MongoDB Search はより高いスコアを割り当てます。
See the Examples section for scoring examples.
例
次の例では、サンプル users コレクションを使用します。
サンプル コレクション
users コレクションには、次の 3 つのドキュメントが含まれます。
db.users.insertMany([ { "_id" : ObjectId("5ed698faa1199b471010d70c"), "name" : "Jim Hall", "verified_user" : true, "account" : { "new_user" : true, "active_user" : true }, "teammates" : [ ObjectId("5ed6990aa1199b471010d70d"), ObjectId("59b99dbdcfa9a34dcd7885c8") ], "region" : "East", "account_created" : ISODate("2021-12-12T10:18:27.000+00:00"), "employee_number" : 257, "uuid" : UUID("fac32260-b511-4c69-8485-a2be5b7dda9e"), "job_title": "engineer" }, { "_id" : ObjectId("5ed6990aa1199b471010d70d"), "name" : "Ellen Smith", "verified_user" : true, "account" : { "new_user" : false, "active_user" : true }, "teammates" : [ ObjectId("5a9427648b0beebeb69537a5"), ObjectId("59b99dbdcfa9a34dcd7881d1") ], "region" : "Southwest", "account_created" : ISODate("2022-05-04T05:01:08.000+00:00"), "employee_number" : 258, "job_title": null }, { "_id" : ObjectId("5ed6994fa1199b471010d70e"), "name" : "Fred Osgood", "verified_user" : false, "account" : { "new_user" : false, "active_user" : false }, "teammates" : [ ObjectId("5a9427648b0beebeb69589a1"), ObjectId("59b99dbdcfa9a34dcd7897d3") ], "region" : "Northwest", "account_created" : ISODate("2022-01-19T08:22:15.000+00:00"), "employee_number" : 259, "job_title": null } ])
{ "_id" : ObjectId("5ed698faa1199b471010d70c"), "name" : "Jim Hall", "verified_user" : true, "account" : { "new_user" : true, "active_user" : true }, "teammates" : [ ObjectId("5ed6990aa1199b471010d70d"), ObjectId("59b99dbdcfa9a34dcd7885c8") ], "region" : "East", "account_created" : ISODate("2021-12-12T10:18:27.000+00:00"), "employee_number" : 257, "uuid" : UUID("fac32260-b511-4c69-8485-a2be5b7dda9e"), "job_title": "engineer" }
{ "_id" : ObjectId("5ed6990aa1199b471010d70d"), "name" : "Ellen Smith", "verified_user" : true, "account" : { "new_user" : false, "active_user" : true }, "teammates" : [ ObjectId("5a9427648b0beebeb69537a5"), ObjectId("59b99dbdcfa9a34dcd7881d1") ], "region" : "Southwest", "account_created" : ISODate("2022-05-04T05:01:08.000+00:00"), "employee_number" : 258, "job_title": null }
{ "_id" : ObjectId("5ed6994fa1199b471010d70e"), "name" : "Fred Osgood", "verified_user" : false, "account" : { "new_user" : false, "active_user" : false }, "teammates" : [ ObjectId("5a9427648b0beebeb69589a1"), ObjectId("59b99dbdcfa9a34dcd7897d3") ], "region" : "Northwest", "account_created" : ISODate("2022-01-19T08:22:15.000+00:00"), "employee_number" : 259, "job_title": null }
サンプル インデックス
usersコレクションは、次のインデックス定義でインデックス付けされます。
{ "mappings": { "dynamic": true, "fields": { "name": { "type": "token", "normalizer": "lowercase" }, "region": [ { "type": "string" }, { "type": "token" } ] } } }
インデックス定義では、以下を指定します。
すべての動的にインデックス付け可能なフィールドを自動的にインデックス化します。
equals演算子を使用したstring検索をサポートするために、nameフィールドをtokenとしてインデックス化します。文字列検索をサポートする場合は
regionフィールドを型stringとしてインデックスし、facet検索をサポートする場合はtokenフィールドを型としてインデックスします。
MongoDB Searchインデックスの作成方法については、 MongoDB Search インデックスの管理 を参照してください。
基本的なクエリの例
ブール値の例
次の例では、 equals演算子を使用して、 usersコレクションからverified_userフィールドがtrueに設定されているドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "verified_user", "value": true } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } } ])
上記のクエリは、次の結果を返します。
{ "name" : "Jim Hall", "score" : 1 } { "name" : "Ellen Smith", "score" : 1 }
「Jim Hall」と「Ellenpass のドキュメントはそれぞれverified_userフィールドがtrueに設定されているため、スコアが1になります。
次の例では、 equals演算子を使用して、 usersコレクション内でaccount.new_userフィールドにブール値trueが含まれるドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "account.new_user", "value": true } } } ])
The preceding query returns the document for "Jim Hall" because that document contains "new_user": true in the account object.
ObjectId の例
次の例では、 equals演算子を使用して、 usersコレクションからteammatesフィールドに値ObjectId("5a9427648b0beebeb69589a1")が含まれるドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69589a1") } } } ])
The preceding query returns the document for "Fred Osgood" because that document contains ObjectId("5a9427648b0beebeb69589a1") in the teammates array.
日付の例
次の例では、 equals演算子を使用して、 usersコレクションからaccount_createdフィールドにISODate("2022-05-04T05:01:08.000+00:00")と一致する値が含まれているドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "account_created", "value": ISODate("2022-05-04T05:01:08.000+00:00") } } } ])
The preceding query returns the document for "Ellen Smith" because that document contains "account_created": 2022-05-04T05:01:08.000+00:00.
数値の例
次の例では、 equals演算子を使用して、 usersコレクションからemployee_numberフィールドに259と一致する値が含まれているドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "employee_number", "value": 259 } } } ])
The preceding query returns the document for "Fred Osgood" because that document contains "employee_number": 259.
string例
次の例では、 equals演算子を使用して、 usersコレクションからnameフィールドにJim Hallと一致する値が含まれているドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "name", "value": "jim hall" } } } ])
The preceding query returns the document for "Jim Hall" because that document contains Jim Hall in the name field. MongoDB Search matches the lowercase query to the uppercase value in the document because it normalizes the term to lowercase as specified in the index definition for the name field.
UUID の例
次の例では、 equals演算子を使用して、 usersコレクションから特定の UUID と一致する値がuuidフィールドに含まれているドキュメントを検索します。
db.users.aggregate([ { "$search": { "equals": { "path": "uuid", "value": UUID("fac32260-b511-4c69-8485-a2be5b7dda9e") } } } ])
The preceding query returns the document for "Jim Hall" because that document contains the specified UUID in the uuid field.
null の例
次の例では、 equals演算子を使用して、 usersコレクションからjob_titleフィールドに null 値が含まれているドキュメントを検索します。
db.users.aggregate([ { $search: { "equals": { "path": "job_title", "value": null } } } ])
The preceding query returns the documents for "Ellen Smith" and Fred Osgood because that documents contain the null value in the job_title field.
複合サンプル クエリ
次の例では、複合演算子をmust、mustNot、およびequalsと組み合わせて、regionフィールドがSouthwestであり、verified_userフィールドがfalseではないドキュメントを検索します。
db.users.aggregate([ { "$search": { "compound": { "must": { "text": { "path": "region", "query": "Southwest" } }, "mustNot": { "equals": { "path": "verified_user", "value": false } } } } } ])
上記のクエリでは、コレクション内で検索条件を満たす唯一のユーザーである "Ellen Salmon" のドキュメントが返されます。
次のサンプル クエリには、これらの検索条件があります。
verified_userフィールドはtrueに設定する必要があります次のいずれか 1 つ を満たす必要があります。
teammates配列には値ObjectId("5ed6990aa1199b471010d70d")が含まれていますregionフィールドはNorthwestに設定されています
db.users.aggregate([ { "$search": { "compound": { "must": { "equals": { "path": "verified_user", "value": true } }, "should": [ { "equals": { "path": "teammates", "value": ObjectId("5ed6990aa1199b471010d70d") } }, { "text": { "path": "region", "query": "Northwest" } } ], "minimumShouldMatch": 1 } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } } ])
上記のクエリは、次の結果を返します。
{ "name" : "Jim Hall", "score" : 2 }
「Jim Hall」のドキュメントは、 must句と 2 つのshould句のうちの最初の句の要件を満たしているため、スコア2を受け取ります。
複合クエリを使用して複数の ObjectID を検索できます。 次のクエリの例では、 compound演算子とshould句を使用して 3 つの異なる ObjectID を検索しています。クエリを満たすには、少なくとも 2 つが表示される必要があります。
db.users.aggregate([ { "$search": { "compound": { "should": [ { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69537a5") } }, { "equals": { "path": "teammates", "value": ObjectId("59b99dbdcfa9a34dcd7881d1") } }, { "equals": { "path": "teammates", "value": ObjectId("5a9427648b0beebeb69579d0") } } ], "minimumShouldMatch": 2 } } }, { "$project": { "name": 1, "_id": 0, "score": { "$meta": "searchScore" } } } ])
上記のクエリは、次の結果を返します。
{ "name" : "Ellen Smith", "score" : 2 }
「Ellen Bridge」のドキュメントは、 teammates配列に指定された ObjectID の 2 つが含まれているため、スコア2を受け取ります。
メタデータの例
次のクエリは、$searchMeta ステージを使用して、users コレクションでverified_users が true であるリージョンの数を取得します。
db.users.aggregate([ { "$searchMeta": { "facet": { "operator": { "equals": { "path": "verified_user", "value": true } }, "facets": { "regionFacet": { "type": "string", "path": "region" } } } } } ])
[ { count: { lowerBound: Long('2') }, facet: { regionFacet: { buckets: [ { _id: 'East', count: Long('1') }, { _id: 'Southwest', count: Long('1') } ] } } } ]
次のクエリは、$$SEARCH_META 集計変数を使用してメタデータと検索結果の両方を返し、users コレクションで verified_users が true であるリージョンの数を取得します。
db.users.aggregate([ { "$search": { "facet": { "operator": { "equals": { "path": "verified_user", "value": true } }, "facets": { "regionFacet": { "type": "string", "path": "region" } } } } }, { "$limit": 2 }, { "$facet": { "docs": [ { "$project": { "type": 1, "description": 1 } } ], "meta": [ {"$replaceWith": "$$SEARCH_META"}, {"$limit": 1} ] } }, { "$set": { "meta": { "$arrayElemAt": ["$meta", 0] } } } ])
[ { docs: [ { _id: ObjectId('5ed698faa1199b471010d70c'), name: 'Jim Hall', region: 'East' }, { _id: ObjectId('5ed6990aa1199b471010d70d'), name: 'Ellen Smith', region: 'Southwest' } ], meta: { count: { lowerBound: Long('2') }, facet: { regionFacet: { buckets: [ { _id: 'East', count: Long('1') }, { _id: 'Southwest', count: Long('1') } ] } } } } ]