0

I am using aggregate to perform some operations, filters and sorting. The problem is that I need to sort (apart from the main sorting) elements (objects) inside an array by one of it's values. I don't want to sort the final result by this array, I just need to sort the array. So, after the lookup of images, I need to sort images by "imageName", and then continue with the operation.

In the result, images are being displayed like this: I want them to appear in correct order depending on the field "imageName"

 "images": [
                {
                    "_id": "5e9cd8f5207ef0c1687da948",
                    "cloudImage": "https:...",
                    "imageName": "image2",
                    "__v": 0,
                    "createdAt": "2020-04-19T23:04:21.582Z",
                    "updatedAt": "2020-04-19T23:04:21.582Z"
                },
                {
                    "_id": "5e9cd8f5207ef0c1687da949",
                    "cloudImage": "https:...",
                    "imageName": "image0",
                    "__v": 0,
                    "createdAt": "2020-04-19T23:04:21.583Z",
                    "updatedAt": "2020-04-19T23:04:21.583Z"
                },
                {
                    "_id": "5e9cd8f5207ef0c1687da94a",
                    "cloudImage": "https:...",
                    "imageName": "image1",
                    "__v": 0,
                    "createdAt": "2020-04-19T23:04:21.584Z",
                    "updatedAt": "2020-04-19T23:04:21.584Z"
                }
            ],

This is the aggregation:

  let query = Shirt
            .aggregate([
                {
                    $lookup: {
                        from: Team.collection.name,
                        localField: 'team',
                        foreignField: '_id',
                        as: 'team',
                    },
                },
                {
                    $lookup: {
                        from: Comment.collection.name,
                        localField: 'comments',
                        foreignField: '_id',
                        as: 'comments',
                    },
                },
                {
                    $lookup: {
                        from: Image.collection.name,
                        localField: 'images',
                        foreignField: '_id',
                        as: 'images',
                    },
                },
                {
                    $addFields: { totalLikes: { "$size": "$likes" } }
                },
                {
                    $unwind: '$team'
                },
                {
                    $match: filters
                },
                {
                    $sort: sorted
                },
                {
                    "$project": {
                        "team": 1,
                        "title": 1,
                        "comments": 1,
                        "size": 1,
                        "year": 1,
                        "brand": 1,
                        "code": 1,
                        "description": 1,
                        "isLikedByUser": 1,
                        "isHome": 1,
                        "isFan": 1,
                        "images": 1,
                        "shirtUser": 1,
                        "likes": 1,
                        "createdAt": 1,
                        "updatedAt": 1
                    },
                },
            ])
            .collation({ locale: "es" })
nacho
  • 531
  • 9
  • 26

1 Answers1

0

You could do this by $unwinding the images array, sorting the resulting docs by imangeName, and then $grouping them back together on _id using the sorted order.

Please refer to below for more details: Mongodb sort inner array