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

124
Views
Sorting documents by sum of specific field in array of other referenced documents

My main schema sort of looks like this:

 _id: id,
 random: random, 
 cards: [objectId, objectId, ...]   //ref to cards

ex of card:

 _id: id,
 random: random, 
 random: random,
 clicks: 15.

I would like to sort the top schema by the sum of clicks within the cards.

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

0

Query1

  • lookup to do the left join, and joined card documents to be an array (join happens if array contains the card _id)
  • sum the clicks
  • sort by the field that has the sum

*if you want descending use sort -1 instead of 1

Test code here

mainColl.aggregate(
[{"$lookup": 
   {"from": "cardCollection",
    "localField": "cards",
    "foreignField": "_id", 
    "as": "clickSum"}},
 {"$set": {"clickSum": {"$sum": "$clickSum.clicks"}}},
 {"$sort": {"clickSum": 1}}])

Query2

  • same like the above but does the calculation before the join (its better but slightly bigger code)

Test code here

mainColl.aggregate(
[{"$lookup": 
    {"from": "cardsCollection",  
      "localField": "cards",
      "foreignField": "_id",
      "pipeline": 
      [{"$group": {"_id": null, "clicksSum": {"$sum": "$clicks"}}}],
      "as": "clicksSum"}},
  {"$set": {"clicksSum": {"$arrayElemAt": ["$clicksSum.clicksSum", 0]}}},
  {"$sort": {"clickSum": 1}}])
about 4 years ago · Juan Pablo Isaza Report

0

I ended up using this:

model.aggregate([ 
          {$lookup: {from: "cards", localField: "cards", foreignField: "_id", as: "cards"}},
          {$set: {clickSum: {$sum: "$cards.clicks"}}},
          {$sort: {clickSum: -1}}
            ])
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!