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)
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.
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.
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.
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
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