Mongo:搜索子数组

时间:2016-08-23 02:14:44

标签: mongodb meteor

寻找属于群组的用户。

以下是一位用户:

meteor:PRIMARY> db.users.find({_id:"rYHRMPpdXRa8fPXrj"})

以下是单个用户的结果:

{ "_id" : "rYHRMPpdXRa8fPXrj", "profile" : { "firstName" : "C", "groups" : [ "GJcqFCnSzfvJbuTMS", "gxD85kTiXmbtwaKfo" ]}

我试着找...

db.users.find({"groups":"rYHRMPpdXRa8fPXrj"})
db.users.find({"profile.groups":"rYHRMPpdXRa8fPXrj"})
db.users.find({profile:{"groups":"rYHRMPpdXRa8fPXrj"}})
db.users.find({"profile":{"groups":"rYHRMPpdXRa8fPXrj"}})
db.users.find({"profile":{"groups":["rYHRMPpdXRa8fPXrj"]}})
db.users.find({profile:{"groups":"rYHRMPpdXRa8fPXrj"}})
db.users.find({profile:{groups:"rYHRMPpdXRa8fPXrj"}})
db.users.find({profile:{groups:["rYHRMPpdXRa8fPXrj"]}})
db.users.find({profile:{$elemMatch:{groups:["rYHRMPpdXRa8fPXrj"]}}})
db.users.find({profile:{$elemMatch:{groups:"rYHRMPpdXRa8fPXrj"}}})
db.users.find({profile:{groups:{$in:["rYHRMPpdXRa8fPXrj"]}}})

我一无所获。 ARRRG !!

如何查找特定个人资料中的所有用户?

2 个答案:

答案 0 :(得分:2)

您将用户_id传递给您的查询而不是组ID(?)。

尝试:

db.users.find({"profile.groups":"GJcqFCnSzfvJbuTMS"})

例如

答案 1 :(得分:-2)

$ all应该用于按数组中的项进行过滤:

db.users.find({profile:{groups:{$all:["rYHRMPpdXRa8fPXrj"]}}})