Q:

How can I correctly output an array of objects in the reverse order from mongodb?

Comments are saved in an array of objects. How can I correctly output them in reverse order (comments from newest to oldest)?
My db:
{"_id":{"$oid":"5e3032f14b82d14604e7cfb7"},
"videoId":"zX6bZbsZ5sU",
"message":[
    {"_id":{"$oid":"5e3032f14b82d14604e7cfb8"},
    "user":{"$oid":"5e2571ba388ea01bcc26bc96"},"text":"1"
    },
     {"_id":{"$oid":"5e3032f14b82d14604e7cfb9"},
    "user":{"$oid":"5e2571ba388ea01bcc26bc96"},"text":"2"
    },
    ....
    ]

My sheme Mongoose:
const schema = new Schema({
videoId: { type: String, isRequired: true },
message: [
{
  user: { type: Schema.Types.ObjectId, ref: 'User' },
  text: { type: String }
},

]
});

My code:
const userComments = await Comment.find(
  { videoId: req.query.videoId },
  { message: { $slice: [skip * SIZE_COMMENT, SIZE_COMMENT] } }
)
  .sort({ message: -1 })
  .populate('message.user', ['avatar', 'firstName']);

but sort not working;
thanks in advance!

A:

You can simply use $reverseArray to reverse content of an array.
db.collection.aggregate([
   {
     $addFields:
      {
         message: { $reverseArray: "$message" }
      }
   }
])

Collection Data :
/* 1 */
{
    "_id" : ObjectId("5e3032f14b82d14604e7cfb7"),
    "videoId" : "zX6bZbsZ5sU",
    "message" : [ 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfb8"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc96"),
            "text" : "1"
        }, 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfb9"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc96"),
            "text" : "2"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5e309318d02e05b694b0b25f"),
    "videoId" : "zX6bZbsZ5sUNEWWWW",
    "message" : [ 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfc9"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc87"),
            "text" : "Old"
        }, 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfd0"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc87"),
            "text" : "New"
        }
    ]
}

Result :
/* 1 */
{
    "_id" : ObjectId("5e3032f14b82d14604e7cfb7"),
    "videoId" : "zX6bZbsZ5sU",
    "message" : [ 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfb9"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc96"),
            "text" : "2"
        }, 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfb8"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc96"),
            "text" : "1"
        }
    ]
}

/* 2 */
{
    "_id" : ObjectId("5e309318d02e05b694b0b25f"),
    "videoId" : "zX6bZbsZ5sUNEWWWW",
    "message" : [ 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfd0"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc87"),
            "text" : "New"
        }, 
        {
            "_id" : ObjectId("5e3032f14b82d14604e7cfc9"),
            "user" : ObjectId("5e2571ba388ea01bcc26bc87"),
            "text" : "Old"
        }
    ]
}

Your Query : You can use native MongoDB's $lookup instead of .populate() , So try below :
Comments.aggregate([
    {
        $addFields:
        {
            message: { $reverseArray: "$message" }
        }
    }, {
        $lookup: {
            from: "User",
            let: { ids: "$message.user" },
            pipeline: [
                {
                    $match: { $expr: { $in: ["$_id", "$$ids"] } }
                },
                { $project: { avatar: 1, firstName: 1, _id: 0 } }
            ],
            as: "userData"
        }
    }
])

