匹配数组内的多个条件

编程入门 行业动态 更新时间:2024-10-07 02:19:59
本文介绍了匹配数组内的多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下几点:

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 } } }}

更多推荐

匹配数组内的多个条件

本文发布于:2023-10-19 01:45:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1506027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   数组   条件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!