【问题标题】:Conditional lookup with array of objects in mongodb在 mongodb 中使用对象数组进行条件查找
【发布时间】:2021-04-26 12:36:04
【问题描述】:

我有两个集合,一个是 products,另一个是 orders 我想在 products 中编写聚合以获得匹配的订单。

产品

[{
 "id": "738097c4-5c52-11eb-ae93-0242ac130002",
 "title": "test product",
 "description": "Amet dolor justo erat sadipscing at sed sit et labore..",
 "combos": ["738097c4", "738097c5"]
},
{
 "id": "923097c4-5c52-11eb-ae93-0242ac1300cj2",
 "title": "test product 2",
 "description": "Acjhz cjzh ouhcio cho ",
 "combos": ["94563097c4", "84097e5"]
}]

订单

[
{
  "id": "ce943752-7040-4926-9c1a-350633f4331f",
  "items": [
    {
      "itemId": "738097c4-5c52-11eb-ae93-0242ac130002",
      "type": "product",
      "expiry": "2021-10-10"
    },
    {
      "itemId": "738097c4",
      "type": "combo",
      "expiry": "2021-12-10"
    }
  ]
},
{
  "id": "33c59dc4-c443-45a7-99c2-caba98f6d107",
  "items": [
    {
      "itemId": "738097c4-5c52-11eb-ae93-0242ac130002",
      "type": "product",
      "expiry": "2022-11-10"
    },
    {
      "itemId": "738097c5",
      "type": "combo",
      "expiry": "2020-10-10"
    }
  ]
}
]

预期输出

产品

[{
 "id": "738097c4-5c52-11eb-ae93-0242ac130002",
 "title": "test product",
 "description": "Amet dolor justo erat sadipscing at sed sit et labore..",
 "combos": ["738097c4", "738097c5"],
 "orders": [
       {
        "id": "ce943752-7040-4926-9c1a-350633f4331f",
        "items": [
                 {
                  "itemId": "738097c4-5c52-11eb-ae93-0242ac130002",
                  "type": "product",
                  "expiry": "2021-10-10"
                 },
                 {
                  "itemId": "738097c4",
                  "type": "combo",
                  "expiry": "2021-12-10"
                 }]
       }].

},
{
 "id": "923097c4-5c52-11eb-ae93-0242ac1300cj2",
 "title": "test product 2",
 "description": "Acjhz cjzh ouhcio cho ",
 "combos": ["94563097c4", "84097e5"],
 "orders:: []
}]

匹配条件

Orders.items.expiry 应该大于当前时间

(任何 Orders.items.itemId 都应匹配 products.id

Orders.items.itemId 应该出现在 products.combos)

请帮我解决

【问题讨论】:

    标签: mongodb mongoose mongodb-query aggregation-framework


    【解决方案1】:

    您可以使用$lookup加入收藏

    脚本是

    db.Order.aggregate(
        [{$addFields: {
          items: {
            $filter:{
              input:"$items",
              cond:{
                $gt:[{$toDate:"$$this.expiry"},new Date()]
              }
            }
          }
        }}, {$lookup: {
          from: 'Products',
          let:{itemIds:"$items.itemId"},
          pipeline:[
            {
              $match:{
                $expr:{
                  $or:[
                      {$in:["$id","$$itemIds"]},
                      {$in:["$combos","$$itemIds"]}
                    ]
                }
              }
            }
            ],
          as: 'join'
        }}]
    )
    

    更新 1

    因为你需要产品的输出

    [{$lookup: {
      from: 'Orders',
      let:{pId:"$id",comboArray:"$combos"},
      pipeline:[
        {$addFields: {
          items: {
            $filter:{
              input:"$items",
              cond:{
                $gt:[{$toDate:"$$this.expiry"},new Date()]
              }
            }
          }
        }},
       {
         $unwind:"$items"
       },
       {
          $match:{
            $expr:{
              $or:[
                  {$eq:["$$pId","$items.itemId"]},
                  {$in:["$items.itemId","$$comboArray"]}
                ]
            }
          }
        },
        {
          $replaceRoot:{
            newRoot:"$items"
          }
        }
    
           ],
      as: 'orders'
    }}]
    

    工作Mongo playground

    【讨论】:

    • 非常感谢您的回答。实际上,我想对产品进行聚合。作为参考,我添加了预期输出。请看看可能会有所帮助
    • 仍然,它在订单字段@varman 中抛出空数组
    • @NagarajVemula 这是工作中的mongoplayground.net/p/rALsdFqCRet 演示
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2017-05-31
    • 2021-04-16
    • 2017-08-22
    • 1970-01-01
    • 2021-11-09
    相关资源
    最近更新 更多