Sort query output in mongoose (find) by two fields

1

I can not sort the result of a query in mongoose and I do not know if an aggregation function is necessary to achieve that order.

I have a column called _id and another column called comment_id (which contains the main _id to reference). This is the query I have:

exports.getCommentsByCollaboratorId = function (req, res) {
    Comments.find({'user_id': req.query._id})
        .populate({
            path: 'user_id',
            select: 'full_name'
        .populate({
            path: 'post_id',
            select: 'title blog_code subcategory'
        })
        .sort({_id: 1, comment_id: 1})
        .exec(function (err, comments) {
            if (err) {
                res.json({status:500, message: 'internal error', type:'internal'});
                return;
            }

            res.json(comments);
            console.log('200       /getCommentsByCollaboratorId');
        })

}

In this query, a join is made for the user and for the post, but when a comment is added it remains at the end when it should be in order from the main comment and all its answers, something like this:

The current one:

The desired one:

Your help is appreciated, greetings.

    
asked by albertodente 29.08.2018 в 15:53
source

0 answers