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

207
Views
Return more counts for same field

Given the following document:

[
  {
    "user": "A",
    "feedback":"like"
  },
  {
    "user": "B",
    "feedback":"dislike"
  },
  {
    "user": "C",
    "feedback":"like"
  },
  {
    "user": "D",
    "feedback":"like"
  },
  {
    "user": "E",
    "feedback":"dislike"
  },
]

I would like to get likes count and dislikes count.

In this case I would get {likesCount: 3, dislikesCount: 2}.

How can I achieve this using Mongoose?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Query

  • assuming that feedback and only be one of those 2 values
  • group by null (all collection 1 group)
    (null is a common choice, the point is to group by a constant "_id" : "hello" would do the same)
  • and 2 sums, if its like add to likesCount 1, else add 0 (the same for the dislikes)

Playmongo

yourColl.aggregate(
[{"$group": 
   {"_id": null,
    "likesCount": 
     {"$sum": {"$cond": [{"$eq": ["$feedback", "like"]}, 1, 0]}},
    "dislikesCount": 
     {"$sum": {"$cond": [{"$eq": ["$feedback", "dislike"]}, 1, 0]}}}}])
about 4 years ago · Juan Pablo Isaza 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!