I\'m trying to use $elemMatch
to find an object in an array. I imported the following data into a collection named trails
:
{ \"Copper\"
$elemmatch(query) returns all rows in a array when there is atleast one row matching the query criteria.
$elemMatch(projection) returns only the first row of all the matching rows when used as projection.
You don't need elemMatch for your case as it is only single criteria.
db.trails.find({"Copper.name": { "Spaulding Bowl" } })
Try as below which uses the elemMatch projection variation.
db.trails.find({}, {"Copper": { $elemMatch: { "name" : "Spaulding Bowl" } } } )