查询Mongo db以获取对象集合中的对象列表

时间:2016-03-20 18:27:02

标签: mongodb

我的收藏是这样的:

{ 
    "_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 个答案:

答案 0 :(得分:1)

通过使用$ elemMatch查询文档内的嵌入式数组,您处于正确的轨道上。以下查询将起作用:

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