2016-03-20 96 views
0

我的收藏是這樣的:查詢蒙戈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" } 
     } 
    ] 
} 

,我想查詢所有具有用戶ID如下集合:

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

但我的查詢一無所獲!我嘗試了以下!

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

我試圖

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

沒有結果!有什麼建議嗎?

回答

1

通過使用$ elemMatch查詢文檔內部的嵌入數組,可以找到正確的軌跡。下面的查詢將工作:

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

THX :)我不得不雙引號添加到烏爾指令,它會像那 db.userPost.find({healthRates:{$ elemMatch:{uid._id: 「HS-56541」}}}) –