本文介绍了Mongo:如果有任何数组位置与单个查询匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假定此数据结构:
{
"title": "Foo bar",
"username": "jackwreid"
},
{
"title": "Bish bosh",
"username": "lizziebump"
},
{
"title": "Ham nam",
"username": "lizziebump"
},
{
"title": "Blub blub",
"username": "jvarn"
}
并假设
var userFollowing = ["lizziebump", "jackwreid"]
对于用户名与userFollowing
数组的任何内容匹配的帖子,我该如何做db.posts.find()
?
How would I do a db.posts.find()
for posts where the username matches any of the contents of the userFollowing
array?
我正在尝试构建一个查询,该查询返回当前用户的以下列表中用户的帖子,并且我只能找到有关如何执行此操作的文档,如果单个查询字符串中包含单个查询字符串,则该方法将以其他方式围绕该查询返回帖子数组位置.
I'm trying to build a query that returns posts by users in the current user's following list and I can only find docs on how to do this the other way around where the query returns posts if a single query string is in any array position.
推荐答案
您可以使用 $in
运算符
You can use the $in
operator
db.collection.find({username: {$in: userFollowing}})
这篇关于Mongo:如果有任何数组位置与单个查询匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!