Reputation: 855
Is it possible to use Mongo to query for entries that have a particular value in a field in an object in an array.
For example, let's say I want to find all objects where field1 has an array of objects, one of which has the field 'one' with a value of 1. This query should return the following object from my collection:
{_id: 0000, field1: [{one: 1, two: 2}, {one: 'uno', two: 'dos'}]}
Upvotes: 25
Views: 38497
Reputation: 311835
This is an old question, but a simpler way to perform this query is to use dot notation:
db.collection.find({'field1.one': 1})
Upvotes: 7
Reputation: 2308
I suppose what you need is:
db.collection.find( { field1: { $elemMatch: { one: 1 } } } );
http://docs.mongodb.org/manual/reference/operator/elemMatch/#op._S_elemMatch
Upvotes: 36