在 mongodb 中查找嵌套数组时过滤条件

问题描述 投票:0回答:1

购物车:

    "_id" : ObjectId("65ad3725957423378d16ae9e"),
    "shopping_cart_items" : [
        {
            "product_id" : ObjectId("65ab7dd8e1d08cf3b831c145"),
            "unit" : "dz",
            "value" : NumberInt(1),
            "quantity" : NumberInt(3)
        },
        {
            "product_id" : ObjectId("65ac956acda553e3e7a0e1a7"),
            "unit" : "qty",
            "value" : NumberInt(1),
            "quantity" : NumberInt(4)
        }
    ]
}

产品:

    "_id" : ObjectId("65ab7dd8e1d08cf3b831c145"),
    "title" : "Hapus Mango",
    "description" : "This is my Hapus",
    "active" : true,
    "measurements" : [
        {
            "sequence" : NumberInt(1),
            "unit" : "dz",
            "value" : NumberInt(1),
            "selling_price" : 120.0,
            "mrp" : 150.0
        },
        {
            "sequence" : NumberInt(2),
            "unit" : "dz",
            "value" : NumberInt(5),
            "selling_price" : 600.0,
            "mrp" : 650.0,
            "purchased_count" : NumberInt(0)
        }
    ]
}
{
    "_id" : ObjectId("65ab828f96a9e5522dd61da1"),
    "title" : "Fresh Banana",
    "description" : "This is my description",
        "active" : true,

    "measurements" : [
        {
            "sequence" : NumberInt(1),
            "unit" : "kg",
            "value" : NumberInt(1),
            "selling_price" : 120.0,
            "mrp" : 150.0
        },
        {
            "sequence" : NumberInt(2),
            "unit" : "gm",
            "value" : NumberInt(500),
            "selling_price" : 60.0,
            "mrp" : 75.0
        },
        {
            "sequence" : NumberInt(3),
            "unit" : "kg",
            "value" : NumberInt(10),
            "selling_price" : 1200.0,
            "mrp" : 1500.0
        }
    ]
}

这些是不同的文档,现在我正在尝试使用Measurement.unit和Measurement.Value过滤器查找带有Product的Shopping_cart,但获取所有测量值

db.getCollection("shopping_cart").aggregate([
{
   "$unwind":"$shopping_cart_items"
},
 { "$lookup": {
      "from": "product",
      "localField":"shopping_cart_items.product_id",
      "foreignField":"_id",
      "let": { "val": "$shopping_cart_items.value","unt": "$shopping_cart_items.unit" },
       "as": "details",
       'pipeline': [
                        {'$match':
                            {'$expr':
                                {'$and': [
                                    {'$eq': ["$$val","$details.measurements.value"]} ,                                  
                                    {'$eq': ["$$unt","$details.measurements.unit"]}   
                                ]
                                }
                            }
                        },
                        {
                            '$project': {"title": 1,"measurements":1}
                        }

                    ],
      "as": "product"
    }}    
])

返回空产品列表,如果我省略匹配条件,则获取完整的测量对象。 如何根据 Shopping_cart_items 的单位和值过滤测量对象。

mongodb aggregation-framework
1个回答
0
投票

通过在项目状态中展开找到答案 '''

db.getCollection("shopping_cart").aggregate([
{
   "$match":{
      "$and":[
         {
            "user":ObjectId("658975a4b361c0fcabb0c6ae")
         },
         {
            "active":true
         }
      ]
   }
},
{
   "$unwind":"$shopping_cart_items"
},
{ "$lookup": {
      "from": "product",
      "let": { cart_product_id:"$shopping_cart_items.product_id", val: "$shopping_cart_items.value",unt: "$shopping_cart_items.unit" },
      pipeline: [
              { $unwind: "$measurements" },
              { $match:
                 { $expr:
                    { $and:
                       [
                         { $eq: [ "$_id",  "$$cart_product_id" ] },
                         { $eq: [ "$measurements.value", "$$val" ] },
                         { $eq: [ "$measurements.unit", "$$unt" ] }
                       ]
                    }
                 }
              }
           ],
           as: "product"
    }
}
   
])

'''

© www.soinside.com 2019 - 2024. All rights reserved.