Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo db - Querying nested array and objects

I have the following document structure:

{    "_id":"12345",    "value":{       "T":0,       "v":[          {             "name":"JW",             "cost":100          }       ]    } } 

How do I query the name key? I tried the dot notation but without luck (I think it works for only two levels)

like image 411
karel lahmy Avatar asked Jul 24 '12 19:07

karel lahmy


People also ask

How do I query a nested object in MongoDB?

Accessing embedded/nested documents – In MongoDB, you can access the fields of nested/embedded documents of the collection using dot notation and when you are using dot notation, then the field and the nested field must be inside the quotation marks.

How do I query an array of objects in MongoDB?

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.

Can we store array of objects in MongoDB?

One of the benefits of MongoDB's rich schema model is the ability to store arrays as document field values. Storing arrays as field values allows you to model one-to-many or many-to-many relationships in a single document, instead of across separate collections as you might in a relational database.


1 Answers

It's not clear exactly what you tried, but this should work to find the above doc by name:

db.collection.find({ "value.v.name": "JW" }) 

Reference

like image 137
JohnnyHK Avatar answered Sep 22 '22 07:09

JohnnyHK