【问题标题】:mongoose filter from subdocuments来自子文档的猫鼬过滤器
【发布时间】:2023-04-02 14:17:01
【问题描述】:

我在 mongodb 中有一个 collection 的订单,其中包含一个名为 line_items 的子文档数组。

我想搜索/过滤line_item.code 与提供的代码匹配的所有订单。 我只想访问 Array not complete 数组中匹配的line_item

我是 mongodb 的新手,尝试了 $elemMatch$filter,但没有成功

【问题讨论】:

  • 请提供示例文档和预期输出。

标签: mongodb mongoose aggregation-framework


【解决方案1】:

没有模型架构和预期的输出,我想你想要这样的东西

db.collection.find(
{
  "line_items.code": 1
},
{
  "line_items.$": 1
})

使用定位操作符$可以得到匹配查询的子文档。

游乐场示例here

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 2015-06-26
    • 2015-10-01
    • 2016-11-23
    • 2012-11-07
    相关资源
    最近更新 更多