简体   繁体   English

在MongoDB中使用$ lookup

[英]Using $lookup in MongoDB

I have the following collections in mydb database: 我的mydb数据库中有以下集合:

  • purchases : it contains documents in the following format: Purchases :包含以下格式的文档:

     { _id: <ObjectId>, name: <String>, //customer name purchasedItems: <Array> 0: < Object > i_name: <String> // item name qntity: <Int> // other objects in the array } 
  • sales : it contains documents in the following format: sales :它包含以下格式的文档:

     { _id: <ObjectId>, i_name: <String>, qntity: <Int> cost: <Int> } 

I'd like to output a new collection that contains the following documents: 我想输出一个包含以下文档的新集合:

    {
     _id: <ObjectId>,
     name: <String>,
     cost: <Int>
    }

Where name is the name of the customer in the purchases collection and cost is the cost of ALL the items that he purchased. 其中name是购买集合中客户的名称,而cost是他购买的所有商品的成本。

Formally, each item's cost is defined as: 正式地,每个项目的成本定义为:

purchases.purchasedItems.qntity/sales.qntity) * sales.cost purchases.purchasedItems.qntity / sales.qntity)*销售成本

WHERE purchases.purchasedItems.i_name=sales.i_name 在哪里Purchases.purchasedItems.i_name = sales.i_name

and cost in the output collection is the sum of all the items' cost. 输出集合中的成本是所有项目成本的总和。

I've tried the following but it doesn't work: 我尝试了以下方法,但是不起作用:

db.purchases.aggregate([
    {$unwind: "$purchasedItems"},
    {$lookup:
        {from:"sales",
        localField:"purchasedItems.i_name",
        foreignField:"i_name",
        as: "n_cost"}
    },
    {
        $group:{
               _id: "$_id",
               name: "$name",
               cost: {$sum: {multiply:[{$divide:["$n_cost.qntity","$qntity"]},"$n_cost.cost"]}}
        }
    },
    {$out: "results"}
])

I'd appreciate any help with what I did wrong and what's the correct way to do it. 对于我做错了什么以及做对的正确方法,我将不胜感激。

So there are couple of things incorrect here. 因此,这里有些错误的地方。

Bunch of missing reference and missing $unwind after $lookup stage. $lookup阶段之后,缺少参考和$unwind束。

Try 尝试

db.purchases.aggregate([
  {"$unwind":"$purchasedItems"},
  {"$lookup":{
    "from":"sales",
    "localField":"purchasedItems.i_name",
    "foreignField":"i_name","as":"n_cost"
  }},
  {"$unwind":"$n_cost"},
  {"$group":{
    "_id":"$_id",
    "name":{"$first":"$name"},
    "cost":{
      "$sum":{
        "$multiply":[
          {"$divide":["$purchasedItems.qntity","$n_cost.qntity"]},
          "$n_cost.cost"
        ]
      }
    }
  }},
  {"$out":"results"}
])

Without $unwind 没有$unwind

db.purchases.aggregate([
  {"$lookup":{
    "from":"sales",
    "localField":"purchasedItems.i_name",
    "foreignField":"i_name",
    "as":"n_cost"
  }},
  {"$project":{
    "name":1,
    "cost":{
      "$sum":{
        "$map":{
          "input":{"$range":[0,{"$size":"$purchasedItems"}]},
          "as":"ix",
          "in":{
            "$let":{
              "vars":{
                "purchase":{"$arrayElemAt":["$purchasedItems","$$ix"]},
                "sales":{"$arrayElemAt":["$n_cost","$$ix"]}},
                "in":{
                  "$multiply":[
                    {"$divide":["$$purchase.qntity","$$sales.qntity"]},"$$sales.cost"
                  ]
                }
            }
          }
        }
      }
    }
  }},
  {"$out":"results"}
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM