0
votes

I am a newbie with mongo and mongoose, I made some search but I can find only examples with complicated schemas. My one is pretty simple, I have a collection with a number of documents like this one:

{
        "_id" : ObjectId("625dc92e20010ca525be6221"),
        "user" : ObjectId("6256946abe645f2e686cc3e3"),
        "title" : "title embedded 2",
        "category" : "nature",
        "isHidden": false,
        "content" : "some text in here...",
        "createdAt" : ISODate("2022-04-18T20:25:18.586Z"),
        "updatedAt" : ISODate("2022-04-18T21:19:20.431Z"),
        "__v" : 0
}

of course I have many categories. I usually query this docs in mongoose like this

let docs = await Document.find({
    isHidden: false
})
.sort({'createdAt': 'desc'})
.populate('user', 'name username');

How can I simply group these documents to get the last one for each category? Thanks.