MongoDB - Project only the matching element in an array MongoDB - Project only the matching element in an array mongoose mongoose

MongoDB - Project only the matching element in an array


Use the positional operator($) to project only the first matching sub document.

db.t.find({"array":{"type":"dog", "name":"Steve"}},{"array.$":1})

Using meteor, you would have to stick to aggregation, since the positional operator does not work:

db.t.aggregate([{$match:{"array.type":"dog","array.name":"Steve"}},{$unwind:"$array"},{$match:{"array.type":"dog","array.name":"Steve"}}])