Sorting array created inside $group stage in MongoDB aggregation pipeline

17 views Asked by At

My MongoDB aggeregation pipeline look like below

      {
        $match: {
          ymd: { $gte: "2024-01-01" },
        },
      },
      {
        $group: {
          _id: { ymd: '$ymd', cityName: '$city.name' },
          products: {
            $addToSet: {
              product: '$product',
            },
          },
        },
      },
      {
        $project: {
          ymd: '$_id.ymd',
          city: '$_id.cityName',
          products: '$products',
        },
      },
      { $sort: { ymd: 1 } },
      {
        $group: {
          _id: '$city',
          data: {
            $push: {
              ymd: '$ymd',
              products: '$products',
            },
          },
        },
      }

After group stage does for each object data array will be sorted according to ymd or not ?

I have gone through some stack overflow post like this and also ran this query from both it comes out that data array will be sorted according to ymd .

Does the above guarantees that data will always be sorted and in no case it will fail?

But I am not able to find any official documentation regarding this.

0

There are 0 answers