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

146
Views
MongoDB - get max 3 values from collections

I would like to ask you for help in finding the maximum dates (limit 3) from all groups.


    collection.aggregate([
        {
            "$group": {
                "_id": "$flashcardCollection",
                "finalMaxDate": { "$max": "$sessionDate" }
            }, 
        },
        { "$sort": { "finalMaxDate": 1 } },
        { "$limit": 3 }
    ]).exec(function(error, fetchAllRecords){
        console.log(fetchAllRecords);
    });

my code finds only one maximum date in each group even though the limit is set to 3

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try this:

db.testCollection.aggregate([
    { $sort: { finalMaxDate: -1 } },
    {
        $group: {
            _id: "$flashcardCollection",
            finalMaxDate: { $push: "$finalMaxDate" }
        }
    },
    {
        $project: {
            finalMaxDate: {
                $slice: ["$finalMaxDate", 3]
            }
        }
    }
])
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!