From 90fce50d15c858fd69fb0daf0d0be010073a3aeb Mon Sep 17 00:00:00 2001 From: Dan Dascalescu Date: Fri, 31 Oct 2014 11:35:52 -0700 Subject: [PATCH] Clarify when $elemMatch is not necessary --- source/reference/operator/query/elemMatch.txt | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/source/reference/operator/query/elemMatch.txt b/source/reference/operator/query/elemMatch.txt index 140ca022b46..04390dc248a 100644 --- a/source/reference/operator/query/elemMatch.txt +++ b/source/reference/operator/query/elemMatch.txt @@ -30,8 +30,8 @@ for :query:`$elemMatch`. Examples -------- -Element Match -~~~~~~~~~~~~~ +Primitive type element match +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Given the following documents in the ``scores`` collection: @@ -87,6 +87,24 @@ Specifically, the query matches the following document: { "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] } + +Single embedded document field +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you only want to find documents with an array containing one (as +opposed to multiple) embedded document fields that satisfy a condition, +then ``$elemMatch`` is not necessary, and you can just write ``array.field``: + + db.survey.find( + { results: { $elemMatch: { product: "xyz" } } } + ) + +is equivalent to: + + db.survey.find( + { "results.product": "xyz" } } } + ) + For more information on querying arrays, see :ref:`read-operations-arrays`, including :ref:`specify-multiple-criteria-for-array-elements` and