匹配数组内的多个条件 [英] Match multiple criteria inside an array
本文介绍了匹配数组内的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下几点:
offers: [
{user: 'jon', price: 200, selected: false},
{user: 'ted', price: 100, selected: true}
]
我正在尝试进行匹配,以获取所有用户被 ted 和选择为 true 的报价对象.
I am trying to do a match that will get all offer objects where user is ted and selected is true.
我尝试了以下方法:
$match: {
"offers.user": "ted"
"offers.selected": true
}
但是,如果数组中有一个 ted 和一个选定的 true 并且在同一对象内不需要该组合,这会给我文档.
But this will give me the document if there is a ted and a selected true inside the array and not necessary that combination inside the same object.
推荐答案
您需要使用 $elemMatch
查询运算符以匹配数组内的多个条件
You need to use $elemMatch
query operator to match multiple criteria inside an array
{ "$match": {
"offers": {
"$elemMatch": {
"user": "ted"
"selected": true
}
}
}}
这篇关于匹配数组内的多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文