How to sort a populated document in find request?

前端 未结 7 1408
挽巷
挽巷 2020-11-28 09:52

I would like to sort a populated Document from the collection i fetch, i get an error requesting it.

Let\'s admit a Document Group (Group) and \'Member\

相关标签:
7条回答
  • 2020-11-28 10:38

    The following worked for me in Mongoose v5.0.5:

        Schedule.find({})
            .populate({path: 'eventComments', options: {sort:{"commentDate": "descending"}}})
            .exec(function(err, result) {
                if (err) {
                    throw err
                } 
    
                else {
                    return res.json(result);
                }
        });

    P.S. The key difference between this example and the Kitten example is that commentDate is in quotes, whereas Date (in the Kitten example) is not. This alteration may be necessary for some of you. Hope this helps.

    0 讨论(0)
提交回复
热议问题