Mongo聚合匹配多个值 [英] Mongo aggregation Match multiple values
本文介绍了Mongo聚合匹配多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下文件
{
name: 'John',
Address :'street 1 '
},
{
name: 'Jane',
Address :'street 2 '
},
{
name: 'Smith',
Address :'street 3 '
}
我想在 mongo 聚合管道中搜索具有不同值的多个文档.
I want to search the multiple document with different vales in mongo aggregation pipeline.
这意味着 name =('John','Smith')
.我期待的结果是
That means name =('John','Smith')
. I'm expecting result is
{
name: 'John',
Address :'street 1 '
},
{
name: 'Smith',
Address :'street 3 '
}
我的代码是
db.articles.aggregate($match: { $or: [{ name: 'John' }, { name: 'Smith' }]);
这是一个空值.是否可以这样获取文档?
This is giving an empty value.is it possible to get document like this way?
推荐答案
需要使用运算符$in
$in 运算符选择字段值所在的文档等于指定数组中的任何值.
[{
$match: {
name: {
$in: ['John', 'Smith']
}
}
}]
这篇关于Mongo聚合匹配多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文