In mongodb, i can find all those records in a collection in database db that contain a particular field using the following query
var doc = db.collection_name.find({field_name:{$exists:true}})
Now consider the following document:
{
"somefield":"someval",
"metadata": {"id":"someval",
"client_url":"http://www.something.com"
}
}
What would be the query for getting all records having the id field in metadata ?
Please Help. Thank You
You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});
The $all operator selects the documents where the value of a field is an array that contains all the specified elements.
You can use dot notation to reference sub-document fields
var doc = db.collection_name.find({"metadata.id":{$exists:true}})
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