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'}]}
To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.
I suppose what you need is:
db.collection.find( { field1: { $elemMatch: { one: 1 } } } );
http://docs.mongodb.org/manual/reference/operator/elemMatch/#op._S_elemMatch
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With