简体   繁体   中英

Nested Mongo DB Query

I Want to calculate profit to do that I need to fetch Item JsonArray which is part of other nested JsonArray Order

To fetch order array objects we can use this query

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()

How can be use this query to fetch items

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

You can access JSON objects with dot or bracket notation, but since the order property contains an array of objects, you'd have to access the array index before you can access the object property inside the array. In this case it would look something like order[index0].item[selectedItemIndex].item_qty/anyOtherItemProperty

Hope this helps, otherwise please provide more details about your issue.

MapReduce is probably not the tool that you need for calculating profit.

To get the overall profit, you might simply unwind the orders and items, and sum it up:

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

Or if you need per-item or per-order profit, it takes a couple more group stages:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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