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

170
Views
How to count document without query

Example I have collection users like this


[
   {
       name : 'John',
       age : 21,
   },
   {
       name : 'Ethan',
       age : 23,
   },
   {
       name : 'Jack',
       age : 21,
   },
]

I want to count how many type of age (result should be 2 because age have 21 and 23) And how many of each type something like db.collection.anyFunctionForCount("help me field age please") result i expect is

{"21" : 1 , "23" : 2}
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to use aggregation-framework in MongoDB default driver, or mongoose. That code should help you:

MongoPlayground example

Model.aggregate([
  {
    "$group": {
      "_id": "$age",
      "field1": {
        "$sum": 1
      }
    }
  }
])
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!