How to query all {"module" : "B"}
?
The following query doesn't work:
db.XXX.find({ "_id" : { "module" : "B" } });
Thanks a ton!
There data looks like:
{ "_id" : {"module" : "A","date" : ISODate("2013-03-18T07:00:00Z")}, "value" : {"count" : 1.0} } { "_id" : {"module" : "B","date" : ISODate("2013-03-18T08:00:00Z")}, "value" : {"count" : 2.0} }
MongoDB Nested Query Match on a Nested Field You can use the dot notation (“field. nestedField”) to specify query criteria for fields in embedded/nested documents. For queries that use dot notation, fields and nested fields must be enclosed in double-quotes.
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.
Try:
db.XXX.find({ "_id.module" : "B" });
The difference is your original query would be trying to match on that entire subdocument (i.e. where _id is a subdocument containing a "module" field with value "B" and nothing else)
Reference: MongoDB Dot Notation
Use dot notation:
db.XXX.find({ "_id.module" : "B" })
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