Docs Menu
Docs Home
/
MongoDB マニュアル
/ / / /

$elemMatch (クエリ)

項目一覧

  • 定義
  • 互換性
  • 構文
  • 動作
  • 詳細

Tip

以下も参照してください。

$elemMatch(プロジェクション)

$elemMatch

$elemMatch 演算子は、指定されたすべてのクエリ条件に一致する要素が少なくとも1つ含まれる配列フィールドを持つドキュメントに一致します。

次の環境でホストされる配置には $elemMatch を使用できます。

  • MongoDB Atlas はクラウドでの MongoDB 配置のためのフルマネージド サービスです

  • MongoDB Enterprise: サブスクリプションベースの自己管理型 MongoDB バージョン

  • MongoDB Community: ソースが利用可能で、無料で使用できる自己管理型の MongoDB のバージョン

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }

次のドキュメントが scores コレクションにあるとします。

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }

次のクエリは、results 配列に80 以上であり、かつ85 未満である要素が少なくとも1 つ含まれるドキュメントのみを一致させます。

db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)

このクエリは、要素 8280 以上で、かつ 85 未満であるため、次のドキュメントが返します。

{ "_id" : 1, "results" : [ 82, 85, 88 ] }

配列要素での複数条件の指定については、配列要素での複数条件の指定を参照してください。

このステートメントは、survey コレクションにドキュメントを挿入します。

db.survey.insertMany( [
{ "_id": 1, "results": [ { "product": "abc", "score": 10 },
{ "product": "xyz", "score": 5 } ] },
{ "_id": 2, "results": [ { "product": "abc", "score": 8 },
{ "product": "xyz", "score": 7 } ] },
{ "_id": 3, "results": [ { "product": "abc", "score": 7 },
{ "product": "xyz", "score": 8 } ] },
{ "_id": 4, "results": [ { "product": "abc", "score": 7 },
{ "product": "def", "score": 8 } ] },
{ "_id": 5, "results": { "product": "xyz", "score": 7 } }
] )

_id5 であるドキュメントは、配列を含みませんが、$elemMatch が配列要素のみと一致することを示すために含まれています。この点については以下で例示します。

次のクエリは、results に少なくとも 1 つの要素が含まれており、product"xyz" であり、score8 以上であるドキュメントに一致します。

db.survey.find(
{ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)

具体的には、このクエリが一致するのは次のドキュメントです。

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }

次のセクションは、単一のクエリ条件で$elemMatchを使用し、 $elemMatchを省略した場合の出力の違いを示します。

$elemMatchを使用したクエリ:

db.survey.find(
{ results: { $elemMatch: { product: "xyz" } } }
)

クエリは、 results 内のいずれかの product"xyz" であるドキュメントを返します。

[
{
_id: 1,
results: [ { product: 'abc', score: 10 }, { product: 'xyz', score: 5 } ]
},
{
_id: 2,
results: [ { product: 'abc', score: 8 }, { product: 'xyz', score: 7 } ]
},
{
_id: 3,
results: [ { product: 'abc', score: 7 }, { product: 'xyz', score: 8 } ]
}
]

$elemMatch がないクエリは以下のようになります。

db.survey.find(
{ "results.product": "xyz" }
)

次の出力には、_id の値が(配列に含まれていない)5 であるドキュメントも含まれていることに注意してください。

[
{
_id: 1,
results: [ { product: 'abc', score: 10 }, { product: 'xyz', score: 5 } ]
},
{
_id: 2,
results: [ { product: 'abc', score: 8 }, { product: 'xyz', score: 7 } ]
},
{
_id: 3,
results: [ { product: 'abc', score: 7 }, { product: 'xyz', score: 8 } ]
},
{ _id: 5, results: { product: 'xyz', score: 7 } }
]

次のクエリを考えてみましょう。

  • 最初のクエリには、<query> という単一条件が $elemMatch に指定されています。

  • 2 つ目のクエリは $elemMatch を省略します。

$elemMatchを使用した最初のクエリは以下のとおりです。

db.survey.find(
{ "results": { $elemMatch: { product: { $ne: "xyz" } } } }
)

クエリは、値が "xyz" 以外である product を含むドキュメントを返します。

{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 },
{ "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 },
{ "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

2 つ目のクエリに $elemMatch がない場合は次のようになります。

db.survey.find(
{ "results.product": { $ne: "xyz" } }
)

クエリでは、 product resultsのいずれも"xyz"であるドキュメントが返されます。

{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

いずれのクエリにも、_id の値が 4 であるドキュメントが含まれており、_id の値が 5 であるドキュメントは、product"xyz" であるため省略されています。

配列のクエリに関するその他の例えは、以下を参照してください。

クエリに関するその他の例えについては、「ドキュメントのクエリ 」を参照してください。

Tip

以下も参照してください。

戻る

$all