Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding all records containing a given subfield in mongodb

Tags:

mongodb

nosql

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

like image 475
Robert Avatar asked Jul 20 '11 20:07

Robert


People also ask

How do I search for a specific field in MongoDB?

You can select a single field in MongoDB using the following syntax: db. yourCollectionName. find({"yourFieldName":yourValue},{"yourSingleFieldName":1,_id:0});

Which operator can be used to Query all the document that contains a given field?

The $all operator selects the documents where the value of a field is an array that contains all the specified elements.


1 Answers

You can use dot notation to reference sub-document fields

var doc = db.collection_name.find({"metadata.id":{$exists:true}})
like image 124
Dan Simon Avatar answered Oct 26 '22 06:10

Dan Simon