mongodb - 检查字段是否是多个值之一 [英] mongodb - check if field is one of many values
本文介绍了mongodb - 检查字段是否是多个值之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果字段值等于数组中的至少一个值,是否有一种有效的方法来查询文档?
Is there an efficient way to query a document by if a field-value is equal to at least one value in an array?
例如当我有以下文件时:
for example when I have the following documents:
{email:"mail1@domain.com"},
{email:"mail2@domain.com"},
.
.
.
{email:"mail1000@domain.com"}
并已索引电子邮件字段,我想获取所有文档的 ID,其电子邮件字段的值等于mail7@domain.com"、mail117@domain.com"或其他 100 个完全不相关的电子邮件地址.可能吗?
and have indexed the email field, I want to get all document's ids, whose email field's value is equal to either "mail7@domain.com","mail117@domain.com" or 100 other totally unrelated email addresses. Is it possible?
我以为会很简单
db.users.find({email:["mail7@domain.com","mail117@domain.com", ... 99 entries ..., "mail 987@domain.com"]})
但我错了.
最有效的方法是什么?
推荐答案
您可以使用 $in
运算符来匹配数组中的任何值:
You can use the $in
operator to match any of the values in an array:
db.users.find(
{email : {$in : ["mail7@domain.com", "mail117@domain.com", "mail 987@domain.com"]}}
)
这篇关于mongodb - 检查字段是否是多个值之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文