【问题标题】:How to do a Mongodb $lookup for local and foreign array fields如何对本地和外部数组字段进行 Mongodb $lookup
【发布时间】:2020-04-13 12:01:39
【问题描述】:

尝试对对象内部的本地数组字段执行 $lookup 。

查询案例集合:

  {
     "no" : "2020921008981",
     "sale" : {
      "soldItems" : [
        {
            "itemId" : "5b55ac7f0550de00210a3b24", 
        },

        {
            "itemId" : "5b55ac7f0550de00215584re", 
        }
      ], 
     "bills" : [
        {
            "billNo" : "2020921053467", 
            "insurancePlanId" : "160", 
        },

        {
            "billNo" : "2020921053467", 
            "insurancePlanId" : "170", 
        }
      ]
    }
  }

物品集合:

{ 
  "_id" : ObjectId("5b55ac7f0550de00210a3b24"), 
  "code" : "ABCDE"
},
{ 
  "_id" : ObjectId("5b55ac7f0550de00215584re"), 
  "code" : "PQRST" 
} 

保险收取:

 { 
   "_id" : ObjectId("5b55aca20550de00210a6d25"), 
   "name" : "HIJKL" 
   "plans" : [
       {
        "_id" : "160", 
        "name" : "UVWZ", 
       }, 
       { 
        "_id" : "161", 
        "name" : "LMNO", 
       }
    ]
  },
 { 
   "_id" : ObjectId("5b55aca20550de00210a6d25"),  
   "name" : "WXYZ"
   "coveragePlans" : [
       {
        "_id" : "169", 
        "name" : "5ABC", 
       }, 
       { 
        "_id" : "170", 
        "name" : "4XYZ", 
       }
    ]
  }

期望的输出:

  {
     "no" : "2020921008981",
     "sale" : {}
     "insurances" : "HIJKL \n WXYZ",
     "items" : [
         { 
            "_id" : ObjectId("5b55ac7f0550de00210a3b24"), 
            "code" : "ABCDE"
       },
         { 
            "_id" : ObjectId("5b55ac7f0550de00215584re"), 
            "code" : "PQRST"
       } 
    ]
  }

尝试使用来自 item 集合的本地 itemRefId 字段进行查找。并使用insurance 集合中的本地insurancePlanId 进行查找,然后将$reduce 返回数组转换为insurances 字段所需的格式:

     {
        $lookup:
            {
                from: "item",
                let:  { iid: "$sale.soldItems.itemId" },
                pipeline: [
                      {
                        $match: {
                            $expr: {
                                $in: ["$_id", {
                                    $map: {
                                        input: "$$iid",
                                        in: { $_id: "$$this" }
                                    }
                                }
                                ]
                            }
                        }
                    }
                ],
                as: "items"
            }
    }, 
  {
        $lookup:
            {
                from: "insurance",
                let:  { iid: "$sale.insurances.insurancePlanId" },
                pipeline: [
                      {
                        $match: {
                            $expr: {
                                $in: ["$insurance.plans._id", {
                                    $map: {
                                        input: "$$iid",
                                        in: { $toObjectId: "$$this" }
                                    }
                                }
                                ]
                            }
                        }
                    }
                ],
                as: "insurancesList"
            }
    }, 
   {
    $addFields: {
        insurances: {
            $reduce: {
                input: "$insurancesList.name",
                initialValue: "",
                in: {
                    $cond: [ { "$eq": [ "$$value", "" ] }, "$$this", { $concat: [ "$$value", "\n", "$$this" ] } ]
                }
            }
        }
    }
}

此尝试返回 mongodb 错误。任何获得所需输出的帮助将不胜感激。

【问题讨论】:

    标签: mongodb aggregation-framework


    【解决方案1】:
    db.case.aggregate([
        {
            $lookup: {
                from: "insurance",
                let: { ipids: "$salesOrder.invoices.insurancePlanId" },
                pipeline: [
                    {
                        $unwind: "$coveragePlans"
                    },
                    {
                        $match: { $expr: { $in: ["$coveragePlans._id", "$$ipids"] } }
                    },
                    {
                        $project: { _id: 0, name: 1 }
                    }
                ],
                as: "insurances"
            }
        },
        {
            $lookup: {
                from: "item",
                let: { iid: "$salesOrder.purchaseItems.itemRefId" },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $in: ["$_id", {
                                    $map: {
                                        input: "$$iid",
                                        in: { $toObjectId: "$$this" }
                                    }
                                }
                                ]
                            }
                        }
                    }
                ],
                as: "items"
            }
        },
        {
            $project: {
                _id: 0,
                caseNumber: 1,
                insurances: {
                    $reduce: {
                        input: "$insurances",
                        initialValue: "",
                        in: { $concat: ["$$value", "$$this.name", " \n "] }
                    }
                },
                items: 1
            }
        }
    ])
    

    【讨论】:

    • 有没有办法以这种方式连接 NumberInt 格式的值?
    • @dineshalwi 不确定哥们,试一试。如果不起作用,请先使用 $toString 进行转换
    • 我们如何对这个查询进行空检查?因为我得到了$in requires an array as a second argument, found: null 的一些记录
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-02-06
    • 2021-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多