Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

162
Views
Calculate total price based on referenced collection

Suppose I have data in bookOrder as:

{
    "_id" : ObjectId("615fc295257d6d7cf57a39fe"),
    "orderId" : "2001",
    "itemId" : [ 
        "615fc232257d6d7cf57a39d4", 
        "615fc251257d6d7cf57a39e0"
    ],
    "Discount" : 10
}

Item data as:

{
    "_id" : ObjectId("615fc232257d6d7cf57a39d4"),
    "itemId" : "1001",
    "Price" : 10.21
} 

{
    "_id" : ObjectId("615fc251257d6d7cf57a39e0"),
    "itemId" : "1002",
    "Price" : 100
}

I want to calculate the total price of order after discount,

i.e. total price as : 100+10.21-10 = 100.21

For this I tried as:

const data = await db.order.aggregate(
        [
            {
                "$match": {
                    "orderId": orderId
                }
            },
            {
                "$lookup": {
                    "from": "item",
                    let: {
                        eid: "$itemId"
                    },
                    pipeline: [
                        {
                            "$match": {
                                $expr: {
                                    $in: [
                                        "$_id",
                                        "$$eid"
                                    ]
                                }
                            }
                        },
                    ],
                    "as": "items"
                }
            },
            {
                "$unwind": {
                    path: "$items"
                }
            },
        ]
    )

So, I get the value as:

{
    "orderId" : "2001",
    "Discount":10,
    "itemId":[{
        "itemId" : "1001",
        "Price" : 10.21
    },
        "itemId" : "1002",
        "Price" : 100
    ]}
}

SO instead of having to loop over the itemId price and get total sun, and then subtracting from the discount price of order can we do all these calculations of db itself.

Is there any way that I can query the total price from DB only instead of having to fetch data and applying any loop and then calculating the total price?

Please let me know if anyone needs any further explanation from my side.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can do this in a couple of ways, here is the most straight forward one using $map and some math operators.

db.order.aggregate([
  {
    "$match": {
      "orderId": "2001"
    }
  },
  {
    "$lookup": {
      "from": "item",
      let: {
        eid: "$itemId"
      },
      pipeline: [
        {
          "$match": {
            $expr: {
              $in: [
                "$_id",
                "$$eid"
              ]
            }
          }
        },
        
      ],
      "as": "items"
    }
  },
  {
    $project: {
      orderId: 1,
      finalSum: {
        $subtract: [
          {
            $sum: {
              $map: {
                input: "$items",
                in: "$$this.Price"
              }
            }
          },
          "$Discount"
        ]
      }
    }
  }
])

Mongo Playground

about 4 years ago · Juan Pablo Isaza Report

0

use sum

aggregate

db.orders.aggregate([
  {
    "$match": {
      "orderId": "2001"
    }
  },
  {
    "$lookup": {
      "from": "items",
      "localField": "itemId",
      "foreignField": "_id",
      "as": "items"
    }
  },
  {
    "$project": {
      "orderId": 1,
      "total": {
        $subtract: [
          {
            "$sum": "$items.Price"
          },
          "$Discount"
        ]
      }
    }
  }
])

mongoplayground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!