简体   繁体   中英

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

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);
});

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, ie 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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM