For AI agents: a documentation index is available at https://www.mongodb.com/ja-jp/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs Menu

compound 演算子

compound

compound 演算子は、2 つ以上の演算子を 1 つのクエリに結合します。compound クエリの各要素は句と呼ばれ、各句は 1 つ以上のサブクエリで構成されます。MongoDB Search には、 ガイダンス付きのサンプル複合クエリ テンプレートが用意されています。

compound の構文は次のとおりです。

1{
2 $search: {
3 "index": <index name>, // optional, defaults to "default"
4 "compound": {
5 <must | mustNot | should | filter>: [ { <clauses> } ],
6 "score": <options>,
7 "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>]
8 }
9 }
10}

Each must, mustNot, should, and filter clause contains an array of subclauses. Use array syntax even if the array contains only one subclause. See the examples on this page.

compound では、次の用語を使用してクエリを作成します。

must

ドキュメントが結果に含まれるために一致する必要がある句。返されるスコアは、句内のすべてのサブクエリのスコアの合計です。

AND ブール値演算子にマップされます。

mustNot

ドキュメントが結果に含まれるためには、一致してはならない句。mustNot 句は返されたドキュメントのスコアに貢献しません。

AND NOT ブール値演算子にマップされます。

should

結果に含まれるドキュメントの中で、一致させたい句。should 句への一致を含むドキュメントは、should 句を含まないドキュメントよりもスコアが高くなります。返されるスコアは、句内のすべてのサブクエリのスコアの合計です。

should 句を複数使用する場合は、minimumShouldMatch オプションを使用して、結果にドキュメントを含めるために一致する必要がある should 句の最小数を指定できます。minimumShouldMatch の値は、クエリ内の should 句の数以下である必要があります。省略した場合、minimumShouldMatch オプションはデフォルトで 0 となります。

を参照してください。

OR ブール値演算子にマップされます。

複合クエリ内で should 句のみを使用する場合、compound 演算子は should 句クエリの配列を論理 OR として扱います。MongoDB Search では、結果を返すために、少なくとも 1 つの should 条件に一致することが必要です。minimumShouldMatch オプションを 0 に設定して、複数の should 句の条件を指定すると、 MongoDB Search は minimumShouldMatch1 に設定されたものとして扱い、結果を返すには少なくとも 1 つの条件に一致する必要があります。

filter

ドキュメントが結果に含まれるために、すべて一致しなければならない句。filter 句は返されたドキュメントのスコアに貢献しません。

たとえば、 compound演算子filterオプションを使用して、 $matchステージを$searchステージに置き換えることができます。 次の$matchステージでは、フィールドroleに特定の値を持つドキュメントをフィルタリングします。

$match: {
"role": { "$in": [ "CLIENT", "PROFESSIONAL" ] }
}

代わりに、 compound演算子filterオプションを使用できます。

$search: {
"compound": {
"filter": [{
"queryString": {
"defaultPath": "role",
"query": "CLIENT OR PROFESSIONAL"
}
}]
}
}

別のフィルターの例を参照してください。

score

句全体の スコア を変更します。compoundscore を使用して、スコアをブースト、置換、または変更できます。 scoreを指定しない場合に返されるスコアは、一致を生成した must 句と should 句のすべてのサブクエリのスコアの合計です。 詳しくは、「 スコアリング動作 」を参照してください。

doesNotAffect

このクエリに基づいてカウントの再計算から除外するファセットまたはファセットのリスト。値は、$search.facets または $searchMeta.facets で定義されたファセットの 1 つ以上の名前である必要があります。演算子は、指定されたファセットを使用してこのクエリに一致するドキュメントをフィルタリングしますが、それらのカウントを再計算しません。詳細については、「 複数選択ファセット 」を参照してください。

クエリ条件の指定には、autocompletetextspan などの任意の最上位の演算子を含む句を使用できます。

To optimize performance, place non-scoring operators such as equals, range, and in in the filter clause to avoid unnecessary scoring operations. If you want to run multiple operations that must be evaluated as an OR condition, nest a should clause within the top-level filter.

[
{
$search: {
index: "default",
compound: {
filter: [
{
compound: {
should: [
{
equals: {
path: "accommodates",
value: 2
}
},
{
range: {
path: "pricePerNight",
lte: 200
}
}
]
}
}
]
}
}
}
]

MongoDB Search は、ドキュメントで一致を生成した個々の句ごとにドキュメントが受け取ったスコアを合計することで、結果セット内のドキュメントをスコアリングします。スコアリングには、must 句と should 句のみが参加します。結果セットは、スコアの高いものから低いものの順に並べられます。

次の表は、スコアに貢献するものと貢献しないものの compound 句を示しています。

スコアに貢献する

filter

must

mustNot

should

You can boost or replace the score of the entire compound query using the score option. For an example of replacing the entire compound score, see Compound Score Example below. You can use score to also boost or alter the score for each subquery in each clause. For some examples of altered scores in compound operator clauses, see Modify the Score.

次の例は、 MongoDB Search Playground または クラスターで試すことができます。

このページの例では、次のドキュメントを含む fruit というコレクションを使用します。

1{
2 "_id" : 1,
3 "type" : "apple",
4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.",
5 "category" : "nonorganic",
6 "in_stock" : false
7},
8{
9 "_id" : 2,
10 "type" : "banana",
11 "description" : "Bananas are usually sold in bunches of five or six.",
12 "category" : "nonorganic",
13 "in_stock" : true
14},
15{
16 "_id" : 3,
17 "type" : "pear",
18 "description" : "Bosc and Bartlett are the most common varieties of pears.",
19 "category" : "organic",
20 "in_stock" : true
21}

fruit コレクションには、コレクション内のすべてのフィールドを自動的にインデックス化し、デフォルトの標準アナライザを使用する動的マッピングを持つデフォルトのインデックスがあります。standard アナライザはすべての単語を小文字にし、よく使われるストップワード("the", "a", "and", など)は無視します。

次のクエリは、 MongoDB Search クエリの $search compound 演算子を示しています。

次の例では、must 句と mustNot 句の組み合わせを使用してクエリを構築します。must 句はテキスト演算子を使用して、description フィールド内の varieties というタームを検索します。ドキュメントが一致するには、must 句を満たす必要があります。mustNot 句は、description フィールド内の apples というタームの検索操作を実行します。ドキュメントが一致するには、mustNot 句を満たさない必要があります。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "must": [{
6 "text": {
7 "query": "varieties",
8 "path": "description"
9 }
10 }],
11 "mustNot": [{
12 "text": {
13 "query": "apples",
14 "path": "description"
15 }
16 }]
17 }
18 }
19 }
20])

上記のクエリでは、description フィールドに varieties という単語が含まれ、apples が含まれていないため、_id: 3 を含むドキュメントが返されます。

MongoDB Search Playground でこれを試してみてください。

次のクエリでは、must を使用して満たす必要のある検索条件を指定し、should を使用して Fuji という単語を含むドキュメントの優先設定を指定します。

このクエリでは、$project パイプライン ステージは _id を除くすべてのドキュメント フィールドを除外し、ドキュメントの関連性スコアを表示する score フィールドを追加します。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "must": [{
6 "text": {
7 "query": "varieties",
8 "path": "description"
9 }
10 }],
11 "should": [{
12 "text": {
13 "query": "Fuji",
14 "path": "description"
15 }
16 }]
17 }
18 }
19 },
20 {
21 "$project": {
22 "score": { "$meta": "searchScore" }
23 }
24 }
25])
{ "_id" : 1, "score" : 0.6425117254257202 }
{ "_id" : 3, "score" : 0.21649497747421265 }

_id: 1 を含むドキュメントは、description フィールドに単語 Fuji が含まれており、should 句を満たしているため、スコアが高くなります。

MongoDB Search Playground でこれを試してみてください。

次のクエリでは、結果内のすべてのドキュメントに対して constant スコア 3 も指定しています。このクエリでは、$project パイプライン ステージは _id を除くすべてのドキュメント フィールドを除外し、score フィールドを追加します。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "must": [{
6 "text": {
7 "query": "varieties",
8 "path": "description"
9 }
10 }],
11 "should": [{
12 "text": {
13 "query": "Fuji",
14 "path": "description"
15 }
16 }],
17 "score": { "constant": { "value": 3 } }
18 }
19 }
20 },
21 {
22 "$project": {
23 "score": { "$meta": "searchScore" }
24 }
25 }
26])
[ { _id: 1, score: 3 }, { _id: 3, score: 3 } ]

クエリの constant オプションによって結果内の各ドキュメントのスコアが数値 3 に置き換えられるため、両方のドキュメントは同じスコアを受け取ります。

MongoDB Search Playground でこれを試してみてください。

複数の should 句を含むクエリでは、miniumumShouldMatch オプションを使用して、結果を返すために一致する必要がある句の最小数を指定できます。

次のクエリには must 句が 1 つと should 句が 2 つあり、minimumShouldMatch の値は 1 です。結果セットに含めるには、ドキュメントの description フィールドに varieties というタームが含まれている必要があり、説明フィールドに Fuji または Golden Delicious のいずれかが含まれている必要があります。

1db.fruit.aggregate([
2 {
3 $search: {
4 "compound": {
5 "must": [{
6 "text": {
7 "query": "varieties",
8 "path": "description"
9 }
10 }],
11 "should": [{
12 "text": {
13 "query": "Fuji",
14 "path": "description"
15 }
16 },
17 {
18 "text": {
19 "query": "Golden Delicious",
20 "path": "description"
21 }
22 }],
23 "minimumShouldMatch": 1
24 }
25 }
26 }
27])
1{
2 "_id" : 1,
3 "type" : "apple",
4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.",
5 "category" : "nonorganic",
6 "in_stock" : false
7}

_id: 1 の一致を含むドキュメントは、must 句と 2 つの should 句のうちの最初の句と一致します。

MongoDB Search Playground でこれを試してみてください。

filter filter 句は返されたドキュメントのスコアには考慮されないため、返されるドキュメントの順序に影響しないことを除いて must と同じように動作します。

次のクエリでは次の句を使用します。

  • must そして filter で、満たす必要のある検索条件を指定します。

  • should banana という単語を含むドキュメントを優先するように指定します。should 句には minimumShouldMatch オプションは含まれていません。minimumShouldMatch を省略すると、デフォルトは 0 となります。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "must": [{
6 "text": {
7 "query": "varieties",
8 "path": "description"
9 }
10 }],
11 "should": [{
12 "text": {
13 "query": "banana",
14 "path": "description"
15 }
16 }],
17 "filter": [{
18 "text": {
19 "query": "granny",
20 "path": "description"
21 }
22 }]
23 }
24 }
25 }
26])
1{
2 "_id" : 1,
3 "type" : "apple",
4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.",
5 "category" : "nonorganic",
6 "in_stock" : false
7}

返されたドキュメントは、含めるためのすべての要件を満たしています。

  • must 句と filter 句の両方が一致します。

  • minimumShouldMatch の値が指定されていないため、デフォルトで 0 になります。その結果、should 句は失敗し、ドキュメントが返されます。

MongoDB Search Playground でこれを試してみてください。

$match$infilter 句を使用して、クラスター上のデータに対するクエリで置き換えることができます。次のクエリは、$search ステージで filter を使用して、一致する必要がある検索用語を指定する方法を示しています。このクエリではまた、should を使用して、varieties というタームを含むドキュメントの優先順位を指定します。このクエリには、次の操作を実行する $projectパイプラインステージが含まれています。

  • _iddescriptionを除くすべてのフィールドを除外します。

  • ドキュメントの関連性スコアを表示する score フィールドを追加します。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "filter": [{
6 "text": {
7 "query": ["apples", "bananas"],
8 "path": "description"
9 }
10 }],
11 "should": [{
12 "text": {
13 "query": "varieties",
14 "path": "description"
15 }
16 }]
17 }
18 }
19 },
20 {
21 "$project": {
22 "description": 1,
23 "score": { "$meta": "searchScore" }
24 }
25 }
26])
1[
2 {
3 _id: 1,
4 description: 'Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp. The most popular varieties are McIntosh, Gala, and Granny Smith.',
5 score: 0.36074575781822205
6 },
7 {
8 _id: 2,
9 description: 'Bananas are usually sold in bunches of five or six.',
10 score: 0
11 }
12]

結果に含まれるドキュメントは、含めるためのすべての要件を満たしています。

  • 両方のドキュメントには、クエリの filter 句で指定された用語 apples または bananas が含まれています。

  • _id: 1 を含むドキュメントは、クエリの should 句で指定された varieties というタームが含まれているため、_id: 2 を含むドキュメントよりもスコアが高くなります。

MongoDB Search Playground でこれを試してみてください。

次の例では、ネストされた compound 句を使用してクエリを構築します。この例では、fruit コレクションには typecategory、および in_stock のフィールドにインデックスがあり、そのテキスト フィールドではデフォルトのアナライザが使用されます。クエリでは、ドキュメントが次の should 句のいずれか 1 つだけを満たす必要があります。

  • type フィールドに apple という単語を含めます。

  • category フィールドに organic というタームが含まれ、in_stock フィールドに true という値が含まれます。

1db.fruit.aggregate([
2 {
3 $search: {
4 "compound": {
5 "should": [
6 {
7 "text": {
8 "query": "apple",
9 "path": "type"
10 }
11 },
12 {
13 "compound": {
14 "must": [
15 {
16 "text": {
17 "query": "organic",
18 "path": "category"
19 }
20 },
21 {
22 "equals": {
23 "value": true,
24 "path": "in_stock"
25 }
26 }
27 ]
28 }
29 }
30 ],
31 "minimumShouldMatch": 1
32 }
33 }
34 }
35])
1{
2 "_id" : 3,
3 "type" : "pear",
4 "description" : "Bosc and Bartlett are the most common varieties of pears.",
5 "category" : "organic",
6 "in_stock" : true
7}
8{
9 "_id" : 1,
10 "type" : "apple",
11 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.",
12 "category" : "nonorganic",
13 "in_stock" : false
14}

結果に含まれるドキュメントは、含まれるためのすべての要件を満たしています。

  • _id: 3 のあるドキュメントは、2 番目の should 句内にネストされている must 句と一致します。

  • _id: 1 のあるドキュメントは最初の should 句に一致します。

MongoDB Search Playground でこれを試してみてください。

次のクエリはメタデータを検索し、$searchMetaパイプラインステージ、または$searchステージの$$SEARCHMETA変数のいずれかを使用して、fruitコレクションのカテゴリの数を返します。

次のクエリは、結果にメタデータのみを返します。

db.fruit.aggregate([
{
"$searchMeta": {
"facet": {
"operator": {
"compound": {
"must": [{
"text": {
"query": "varieties",
"path": "description"
}
}],
"should": [{
"text": {
"query": "Fuji",
"path": "description"
}
}]
}
},
"facets": {
"categoryFacet": {
"type": "string",
"path": "category"
}
}
}
}
}
])
[
{
count: { lowerBound: Long('2') },
facet: {
categoryFacet: {
buckets: [
{ _id: 'nonorganic', count: Long('1') },
{ _id: 'organic', count: Long('1') }
]
}
}
}
]

次のクエリはメタデータと検索結果を返します。

db.fruit.aggregate([
{
"$search": {
"facet": {
"operator": {
"compound": {
"must": [{
"text": {
"query": "varieties",
"path": "description"
}
}],
"should": [{
"text": {
"query": "Fuji",
"path": "description"
}
}]
}
},
"facets": {
"categoryFacet": {
"type": "string",
"path": "category"
}
}
}
}
},
{ "$limit": 2 },
{
"$facet": {
"docs": [
{ "$project":
{
"type": 1,
"description": 1
}
}
],
"meta": [
{"$replaceWith": "$$SEARCH_META"},
{"$limit": 1}
]
}
},
{
"$set": {
"meta": {
"$arrayElemAt": ["$meta", 0]
}
}
}
])
[
{
docs: [
{
_id: 1,
type: 'apple',
description: 'Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.'
},
{
_id: 3,
type: 'pear',
description: 'Bosc and Bartlett are the most common varieties of pears.'
}
],
meta: {
count: { lowerBound: Long('2') },
facet: {
categoryFacet: {
buckets: [
{ _id: 'nonorganic', count: Long('1') },
{ _id: 'organic', count: Long('1') }
]
}
}
}
}
]