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

174
Views
MongoDB Aggregation: Counting distinct fields from array

Need to count distinct tags on all registers in a mongodb query, using JS.

Register structure:

  {
  "_id": {
    "$oid": "62e593aed8fd9808777225e8"
  },
  "title": "“The world as we have created it is a process of our thinking. It cannot be changed without changing our thinking.”",
  "author": {
    "name": "Albert Einstein",
    "url": "https://quotes.toscrape.com/author/Albert-Einstein"
  },
  "tag": [
    "change",
    "deep-thoughts",
    "thinking",
    "world"
  ]
  }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This could be useful. In addition to get the different values for the field, it returns the number of appearances:

db.collection.aggregate([
  {
    "$unwind": "$tag"
  },
  {
    "$group": {
      "_id": "$tag",
      "total": {
        "$sum": 1
      }
    }
  },
  
])

You can try here: https://mongoplayground.net/p/yXLYkJKO3Wf

about 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!