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

448
Views
Mongoose/MongoDB: $in and .sort()

I hit an API which follows 50 members' data in a game once a day, and use mongoose to convert the JSON into individual documents in a collection. Between days there is data which is consistent, for example each member's tag (an id for the member in game), but there is data which is different (different scores etc.). Each document has a createdAt property.

I would like to find the most recent document for each member, and thus have an array with each member's tag.

I an currently using the following query to find all documents where tags match, however they are returning all documents, not just one. How do I sort/limit the documents to the most recent one, whilst keep it as one query (or is there a more "mongodb way")?

memberTags = [1,2,3,4,5];
ClanMember.find({
    'tag': {
        $in: memberTags
    }
}).lean().exec(function(err, members) {
    res.json(members);
});

Thanks

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can query via the aggregation framework. Your query would involve a pipeline that has stages that process the input documents to give you the desired result. In your case, the pipeline would have a $match phase which acts as a query for the initial filter. $match uses standard MongoDB queries thus you can still query using $in.

The next step would be to sort those filtered documents by the createdAt field. This is done using the $sort operator.

The preceding pipeline stage involves aggregating the ordered documents to return the top document for each group. The $group operator together with the $first accumulator are the operators which make this possible.

Putting this altogether you can run the following aggregate operation to get your desired result:

memberTags = [1,2,3,4,5];
ClanMember.aggregate([
    { "$match": { "tag": { "$in": memberTags } } },
    { "$sort": { "tag": 1, "createdAt: -1 " } },
    {
        "$group": {
            "_id": "$tag",
            "createdAt": { "$first": "$createdAt" } /*,
            include other necessary fields as appropriate
            using the $first operator e.g.
            "otherField1": { "$first": "$otherField1" },
            "otherField2": { "$first": "$otherField2" },
            ...

            */
        }
    }
]).exec(function(err, members) {
    res.json(members);
});

Or tweak your current query using find() so that you can sort on two fields, i.e. the tag (ascending) and createdAt (descending) attributes. You can then select the top 5 documents using limit, something like the following:

memberTags = [1,2,3,4,5];
ClanMember.find(
    { 'tag': { $in: memberTags } }, // query
    {}, // projection
    { // options
        sort: { 'createdAt': -1, 'tag': 1 },
        limit: memberTags.length, 
        skip: 0
    }
).lean().exec(function(err, members) {
    res.json(members);
});

or

memberTags = [1,2,3,4,5];
ClanMember.find({
    'tag': {
        $in: memberTags
    }
}).sort('-createdAt tag')
  .limit(memberTags.length)
  .lean()
  .exec(function(err, members) {
    res.json(members);
});
over 4 years ago · Santiago Trujillo Report

0

Ok, so, first, let's use findOne() so you get only one document out of the request Then to sort by the newest document, you can use .sort({elementYouWantToSort: -1}) (-1 meaning you want to sort from newest to oldest, and 1 from the oldest to the newest) I would recommend to use this function on the _id, which already includes creation date of the document Which gives us the following request :

ClanMember.findOne({
    'tag': {
        $in: memberTags
    }
}).sort({_id: -1}).lean().exec(function(err, members) {
    res.json(members);
});
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!