Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

460
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda