sonstiq
sonstiq

Reputation: 73

Mongodb returns no result on any match query

My data is like

myData[
    {
       id : 1,
       name : "a1",
       order : {
         id : 1,
         name : "o1",
         status : "2"
       }
    },
    {
       id : 2,
       name : "a2",
       order : {
         id : 1,
         name : "o1",
         status : "2"
       }
    },
    {
       id : 3,
       name : "a1",
       order : {
         id : 1,
         name : "o2",
         status : "3"
       }
    }
    ]

when I tried to query :

db.myData.find({
   "order" : {
      id : 1
   }
})

Same as

db.myData.find({ "order.id" :  1})

I expect 3 results but it returns nothing and when I tried to

db.myData.find({
   "order" : {
         id : 1,
         name : "o2",
         status : "3"
   }
})

it returns a result. Where is the problem?

Upvotes: 1

Views: 271

Answers (2)

Mohit Garg
Mohit Garg

Reputation: 86

db.mydata.find({"order.id" : 1})

This query should work fine. It looks like you are somewhere overriding default _id field behaviour of your document.

Upvotes: 0

Salvador Dali
Salvador Dali

Reputation: 222999

I think you are doing something wrong.

Definitely the db.myData.find({ "order" : { id : 1 } }) should return nothing, because it looks for a specific subdocument {id : 1}.

On the other hand the second query should work correctly: db.myData.find({ "order.id" : 1}) and especially if db.myData.find({ "order" : { id : 1, name : "o2", status : "3" } }) gives you something.

Upvotes: 1

Related Questions