Finding all records containing a given subfield in

2019-02-11 23:13发布

问题:

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

回答1:

You can use dot notation to reference sub-document fields

var doc = db.collection_name.find({"metadata.id":{$exists:true}})


标签: mongodb nosql