如何过滤Mongodb $ lookup结果以仅获取匹配的嵌套对象?

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

我有一个customers集合,例如;

{ 
  "_id" : ObjectId("5de8c07dc035532b489b2e23"),
  "name" : "sam",
  "orders" : [{"ordername" : "cola"},{"ordername" : "cheesecake"}]
}

waiters集合;例如;

{   
    "_id" : ObjectId("5de8bc24c035532b489b2e20"),
    "waiter" : "jack",
    "products" : [{"name" : "cola", "price" : "4"}, 
                  {"name" : "water", "price" : "2"}, 
                  {"name" : "coffee", "price" : "8" }]
}
{   
    "_id" : ObjectId("5de8bdc7c035532b489b2e21"),
    "waiter" : "susan",
    "products" : [{"name" : "cheesecake", "price" : "12" }, 
                  {"name" : "apple pie", "price" : "14" }]
}

我想通过匹配“ products.name”和“ orders.ordername”将waiters集合中的对象加入customers集合中。但是,结果包括waiters集合中的整个文档,但是,我只想要文档中匹配的对象。这就是我想要的;

ordered:[ 
  {"name" : "cola", "price" : "4"},
  {"name" : "cheesecake", "price" : "12" },
]

我尝试了$lookup,带有和不带有管道,以及过滤器,但无法获得此结果。预先感谢。

mongodb mongodb-query aggregation-framework
1个回答
1
投票

您的想法正确,由于结构类似,我们只需要稍微“整理”数据即可:

db.collection.aggregate([
    {
        $addFields: {
            "orderNames":
                {
                    $reduce: {
                        input: "$orders",
                        initialValue: [],
                        in: {$concatArrays: [["$$this.ordername"], "$$value"]}
                    }
                }
        }
    },
    {
      $lookup:
          {
            from: "waiters",
            let: {orders: "$orderNames"},
            pipeline: [
                {
                    $unwind: "$products"
                },
                {
                    $match:
                        {
                            $expr:{$in: ["$products.name", "$$orders"]},
                        }
                },
                {
                    $group: {
                        _id: "$products.name",
                        price: {$first: "$products.price"}
                    }
                },
                {
                    $project: {
                        _id: 0,
                        price: 1,
                        name: "$_id"
                    }
                }
            ],
            as: "ordered"
        }
    }
])
  • 感觉您可以从一组新的将价格映射到价格的项目中受益。可以节省大量时间。
© www.soinside.com 2019 - 2024. All rights reserved.