【问题标题】:Nested Mongo DB Query嵌套的MongoDB查询
【发布时间】:2021-06-20 19:54:11
【问题描述】:

我想计算利润,我需要获取 Item JsonArray,它是其他嵌套 JsonArray Order 的一部分

要获取订单数组对象,我们可以使用这个查询

var mapProfit=function(){for(var i in this.order)emit(this.order[i],1)}
var redProfit=function(k,v){return Array.sum(v)}
db.OnlineGrocery.mapReduce(mapProfit,redProfit,{out:"Result"}).find()

如何使用此查询来获取项目

{ 
    "_id" : ObjectId("605ac4dcf77f914c2aab81b0"), 
    "customer_id" : NumberInt(1), 
    "customer_firstName" : "Lannie", 
    "customer_lastName" : "Chazerand", 
    "customer_email" : "lchazerand0@usnews.com", 
    "customer_phoneNumber" : "3862602788", 
    "address" : [
        {
            "street" : "00523 Helena Plaza", 
            "city" : "Cincinnati", 
            "province" : "Ohio"
        }
    ], 
    "order" : [
        {
            "order_id" : "98-037-3943", 
            "order_date" : "6/22/2020", 
            "item" : [
                {
                    "item_id" : NumberInt(1), 
                    "item_name" : "Appetizer - Mango Chevre", 
                    "item_desc" : "Nondisp fx of pisiform, unsp wrist, init for clos fx", 
                    "item_qty" : NumberInt(5), 
                    "item_actual_price" : "$2.78", 
                    "item_selling_price" : "$8.23"
                }, 
                {
                    "item_id" : NumberInt(2), 
                    "item_name" : "Pork - Bacon,back Peameal", 
                    "item_desc" : "Other cervical disc disorders at C6-C7 level", 
                    "item_qty" : NumberInt(2), 
                    "item_actual_price" : "$1.53", 
                    "item_selling_price" : "$6.71"
                }
         ]
    }]
}

【问题讨论】:

  • 你到底需要什么
  • 你的意思是总结所有item_selling_price
  • 到目前为止你尝试了什么?
  • 要获取订单数组对象,我们可以使用这个查询 var mapProfit=function(){for(var i in this.order)emit(this.order[i],1)} var redProfit=function (k,v){return Array.sum(v)} db.OnlineGrocery.mapReduce(mapProfit,redProfit,{out:"Result"}).find() 如何使用这个查询来获取项目

标签: javascript json mongodb aggregate-functions nested-query


【解决方案1】:

您可以使用点或括号表示法访问 JSON 对象,但由于 order 属性包含一个对象数组,因此您必须先访问数组索引,然后才能访问数组中的 object 属性。在这种情况下,它看起来像 order[index0].item[selectedItemIndex].item_qty/anyOtherItemProperty

希望这会有所帮助,否则请提供有关您的问题的更多详细信息。

【讨论】:

    【解决方案2】:

    MapReduce 可能不是您计算利润所需的工具。

    要获得整体利润,您可以简单地展开订单和项目,然后总结:

    db.collection.aggregate([
      {$unwind: "$order"},
      {$unwind: "$order.item"},
      {$group: {
          _id: null,
          profit: {
            $sum: {
              $subtract: [
                {$toDecimal: {
                    $trim: {
                      chars: {$literal: "$"},
                      input: "$order.item.item_selling_price", 
                    }
                }},
                {$toDecimal: {
                    $ltrim: {
                      input: "$order.item.item_actual_price",
                      chars: {$literal: "$"}
                    }
                }}
              ]
            }
          }
      }}
    ])
    

    Playground

    或者,如果您需要按商品或按订单获利,则需要更多的小组阶段:

    db.collection.aggregate([
      {$unwind: "$order"},
      {$unwind: "$order.item"},
      {$addFields: {
          "order.item.profit": {
            $subtract: [
                {$toDecimal: {
                  $trim: {
                    chars: {$literal: "$"},
                    input: "$order.item.item_selling_price", 
                  }
                }},
                {$toDecimal: {
                  $ltrim: {
                    input: "$order.item.item_actual_price",
                    chars: {$literal: "$"}
                  }
                }}
            ]
          }
      }},
      {$group: {
          _id: {
            _id: "$_id",
            order: "$order.order_id"
          },
          root: {$first: "$$ROOT"},
          items: {$push: "$order.item"},
          profit: {$sum: "$order.item.profit"}
      }},
      {$addFields: {
          "root.order.item": "$items",
          "root.order.profit": "$profit"
      }},
      {$replaceRoot: {newRoot: "$root"}},
      {$group: {
          _id: "$_id",
          root: {$first: "$$ROOT"},
          orders: {$push: "$order"}
       }},
      {$addFields: {"root.order": "$orders"}},
      {$replaceRoot: {newRoot: "$root"}}
    ])
    

    Playground

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 2015-11-19
      • 2020-02-26
      • 2019-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-22
      相关资源
      最近更新 更多