37

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}
}
1
  • This is very wierd, document identifier (_id) field Commented Mar 26, 2013 at 8:40

3 Answers 3

69

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

Sign up to request clarification or add additional context in comments.

Comments

3

Use dot notation:

db.XXX.find({ "_id.module" : "B" })

Comments

2

For Exact match on Subdocument

    db.bios.find(
   {
     '_id.module': 'B'
   }
)

the query uses dot notation to access fields in a subdocument:

Refference link

1 Comment

You've got an extra quote in there after the B (or a missing one before it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.