0
{
        "id" : "12345"
        "energy" : "electricity",
        "type" : "lighting",
        "value" : 24324
    }, 
    {
        "id" : "12345"
        "energy" : "electricity",
        "type" : "equipment",
        "value" : 24324
    }, 
    {
        "energy" : "electricity",
        "type" : "fans",
        "value" : 24324
    }, 
    {
        "id" : "12345"
        "energy" : "electricity",
        "type" : "distribution",
        "value" : 24324
    }, 
    {
        "id" : "12345"
        "energy" : "electricity",
        "type" : "chiller",
        "value" : 24324
    }, 
    {
       "id" : "12345"
        "energy" : "electricity",
        "type" : "other",
        "value" : 24324
    }

This is my query to find total value. I use $project.

  db.invoices.aggregate([
    {
    $project: {
        total: {$sum : "$value"}
    }
}])

I try to find the total value. Can any one please help to find the total value. also find the value with the Id wise.

3
  • db.products.aggregate([{$group:{_id:'$type',total:{$sum:'$value'}}}]) Commented May 12, 2018 at 7:54
  • Thanks @Joel Garcia Nuño but how do I find the total that Id: "12345" Commented May 12, 2018 at 8:30
  • Try to filter in the _id: <expresion> , gonna be equals than 12345 id, but one of them doesn't have it, may you need to check if it's null don't do anything Commented May 12, 2018 at 11:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.