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

198
Views
Grab count of a specific field in database

So I have a schema, and in that schema there is an object called "caseCount" with a Number Value. I simply want to fetch every value for each object of "caseCount" and add it up. Could somebody point me in the right direction? If I need to explain some more, I'm happy to do so. Thank you!

Schema:

const Profile = Schema({
    userID: String,
    messageCount: Number,
    caseCount: Number,
    Skins: Array,
});

code:

const data = Profiles.collection.aggregate([{
      $group: {
        _id: '$id',
        totalCaseCount: { $sum: '$caseCount' }
      }
    }]);

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Demo - https://mongoplayground.net/p/7TnpQl3tJHt

Use $sum in aggregation query.

$group

db.collection.aggregate({
  $group: {
    _id: null,
    toatalCaseCount: {  $sum: "$caseCount" }
  }
})

Profiles.collection.aggregate([{
  $group: {
    _id: null,
    totalCaseCount: { $sum: '$caseCount' }
  }
  }], function(err, data) {
   console.log(data); // here you'll get your data from the query
});
over 4 years ago · Santiago Trujillo 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!