[英]Collection.find() for array inside object
I have a Meteor app that has a collection of chat groups. 我有一个Meteor应用,其中包含聊天群组的集合。 Each document in this collection looks like this:
此集合中的每个文档如下所示:
{groupname: 'name', whitelist: ['person1', 'person2', 'person3'], messages:['message1', 'message2', 'message3']}
To show each person with an account what groups they are part of, I need a page with a list of those groups they're allowed in. How can I use a find() command to return all of the documents whose whitelist array contains the user's name? 要向每个有帐户的人显示他们属于哪个组,我需要一个页面,其中包含允许他们进入的那些组的列表。如何使用find()命令返回所有白名单数组包含以下内容的文档:用户名?
Something like this: 像这样:
ChatGroups.find({whitelist: Meteor.user().username});
This assumes that username
is the property you want to match against. 假设
username
是您要匹配的属性。 You don't need to do anything special to search within an array in this case - mongo will do the correct thing (compare username
to each element of each whitelist
and return those documents that match). 在这种情况下,您无需执行任何特殊操作即可在数组中进行搜索-mongo将做正确的事(将
username
与每个whitelist
每个元素进行比较,并返回匹配的文档)。
To search in MongoDB by array items - use $in
operator: 要按数组项在MongoDB中搜索-使用
$in
运算符:
ChatGroups.find({
whitelist: {
$in: [Meteor.user()._id]
}
});
Reference: Comparison Query Operators > $in 参考: 比较查询运算符> $ in
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.