How do i find the sum of a field in Mongo with specific conditions?

44 Views Asked by At

I have collection with below documents:

_id : "123"
name : "rahul"
age : 40
experience: "10"


_id : "124"
name : "manish"
age : 42
experience: "12"

I want to find the sum of all the ages and also experience:

I am using below query:

db.emp_data.aggregate({
      $group:
        {
          _id: null,
          totalCap: {
            $sum: "$age",
          },
        },
    })

But , i am getting empty records. What condition should i put in the _id field?

0

There are 0 best solutions below