How use the return of $elementMatch with Projection

How we can get only the document item that is the result of $elementMatch. Ex: In the M001 > results.surveys > run this: {results: {$elemMatch: {product: “abc”, score: 7}}} --> You get as a result: _id:5964e61ff0df64e7bc2d71ce
results:
0:
product:“abc”
score:7
1:
product:“xyz”
score:7

But, I need only the result below:
0:
product:“abc”
score:7
How can I get that?

Hi @evandroo,

You are getting this result because when you issue a query, the complete document that matches the query filter is displayed.

To get this result you need to use projections.

In your query, you need to add the projection filter to display only limited fields as follows:

db.surveys.find({results: {$elemMatch: {product: "abc", score: 7}}}, {"results.$":1, _id:0})

I hope it helps!!

Please let me know, if you have any questions.

Thanks,
Sonali

1 Like

Hope that runs: db.surveys.find({results: {$elemMatch: {"product": {$eq: "abc"}, "score": 7}}}).count()