2016-03-20 19 views
0

benim toplama şu şekildedir:sorgu Mongo db

{ 
    "_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" } 
     } 
    ] 
} 

ve ben şöyle bir kullanıcı kimliğine sahip tüm koleksiyonu için sorgulamak istiyorum:

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

ama benim sorgum hiçbir şey almaz! Aşağıdakileri denedim!

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

ve ben bir sonucu olmadan

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

çalıştı! herhangi bir öneri lütfen?

cevap

1

Belgenizin içine yerleştirilmiş diziyi sorgulamak için $ elemMatch kullanarak doğru yoldasınız. Aşağıdaki sorgu çalışır:

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

Thx :) ben sadece ur talimat çift tırnak eklemek zorunda ve bu db.userPost.find ({healthRates gibi çalışır: {$ elemMatch: {uid._id: "HS-56.541"}}}) –