嵌套的 Mongo DB 查询

时间:2021-03-24 05:48:00

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

我想计算利润,我需要获取项目 JsonArray,它是其他嵌套 JsonArray 订单的一部分

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

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"
                }
         ]
    }]
}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

相关问题