$elemMatch (query)
Definição
$elemMatch
The
$elemMatch
operator matches documents that contain an array field with at least one element that matches all the specified query criteria.
Compatibilidade
Você pode utilizar o $elemMatch
para implantações hospedadas nos seguintes ambientes:
MongoDB Atlas: o serviço totalmente gerenciado para implantações do MongoDB na nuvem
MongoDB Enterprise: a versão autogerenciada e baseada em assinatura do MongoDB
MongoDB Community: uma versão com código disponível, de uso gratuito e autogerenciada do MongoDB
Sintaxe
{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
Comportamento
You cannot specify a
$where
operator in an$elemMatch
.Você não pode especificar um operador de consulta
$text
em um$elemMatch
.
Exemplos
Element Match
Given the following documents in the scores
collection:
{ _id: 1, results: [ 82, 85, 88 ] } { _id: 2, results: [ 75, 88, 89 ] }
The following query matches only those documents where the results
array contains at least one element that is both greater than or equal
to 80
and is less than 85
:
db.scores.find( { results: { $elemMatch: { $gte: 80, $lt: 85 } } } )
The query returns the following document because the element 82
is
both greater than or equal to 80
and is less than 85
:
{ "_id" : 1, "results" : [ 82, 85, 88 ] }
For more information on specifying multiple criteria on array elements, see Especificar várias condições para elementos de Array.
Array of Embedded Documents
This statement inserts documents into the survey
collection:
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 } } ] )
The document with an _id
of 5
doesn't contain an array. That
document is included to show that $elemMatch
only matches array
elements, which you will see in the following examples.
The following query matches documents where results
contains at
least one element where product
is "xyz"
and score
is
greater than or equal to 8
:
db.survey.find( { results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } } )
Specifically, the query matches the following document:
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "xyz", "score" : 8 } ] }
Single Query Condition
The following sections show the output differences when you use
$elemMatch
with a single query condition, and omit $elemMatch
.
Exemplo 1
Query with $elemMatch
:
db.survey.find( { results: { $elemMatch: { product: "xyz" } } } )
The query returns documents where any product
in results
is
"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 } ] } ]
Query without $elemMatch
:
db.survey.find( { "results.product": "xyz" } )
In the following output, notice that the document with an _id
of
5
(which doesn't contain an array) is also included:
[ { _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 } } ]
Exemplo 2
Consider the following queries:
First query has a single
<query>
condition in$elemMatch
.Second query omits
$elemMatch
.
First query with $elemMatch
:
db.survey.find( { "results": { $elemMatch: { product: { $ne: "xyz" } } } } )
The query returns documents that has a product
with value other than
"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 } ] } { "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "def", "score" : 8 } ] }
Second query without $elemMatch
:
db.survey.find( { "results.product": { $ne: "xyz" } } )
The query returns documents where none of the product
results
are "xyz"
:
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 }, { "product" : "def", "score" : 8 } ] }
Both queries include the document with an _id
of 4
, and omit the
document with an _id
of 5
because the product
is "xyz"
.
Saiba mais
Para obter exemplos adicionais sobre como consultar arrays, consulte:
Para obter exemplos adicionais sobre queries, consulte Fazer query em documentos