Elmu Ezz
Elmu Ezz

Reputation: 61

query Mongo db for list of object inside object collection

my collection is like this:

{ 
    "_id" : ObjectId("56b888ae31c1d1d3bf79f8c6"),
     "_class" : "fore.server.domain.post.UserPost", 
    "image" : { "_id" : null, "statusText" : "This food i cooked" },
    "healthRates" : [
        { 
        "_id" : null, 
        "rateValue" : "VerryPoor",
        "uid" : { "_id" : "HS-56541" }
        }
    ]
}

and i want to query for all the collection which has the user id as follows:

{"uid" : { "_id" : "HS-56541" }} 

but my query get nothing !! i tried the following !

db.userPost.find({healthRates:{$elemMatch: "uid" : { "_id" : "HS-56541" } } )

and i tried

db.userPost.find({"healthRates.uid.id" : "HS-56541" } )

without a result! any suggestion please?

Upvotes: 0

Views: 968

Answers (1)

M. Mennan Kara
M. Mennan Kara

Reputation: 10222

You were on correct track by using $elemMatch to query the embedded array inside of your document. The following query will work:

db.userPost.find({healthRates:{$elemMatch:{uid._id:"HS-56541"}}})

Upvotes: 1

Related Questions