Sort collection on document ordernumber and sort subarray on subarray objectorder

I’m afraid that’s a huge anti-pattern as once you do the $unwind both the $sort and the $group will need to be in memory or worse still useDisk operations and use huge amounts of RAM.

It is possible to do this on the server where you sort the array in place using aggregation although it’s fairly advanced , most good Aggregation code needs code to generate it.

The code at [bits-n-pieces/sortArray.js at master · asya999/bits-n-pieces · GitHub] will generate the aggregation pipeline you need, you can run it in a mongo shell or in node. @Asya_Kamsky also did a talk about this which you can find online.

So in your case you basically want to (a) sort by documentordernumber using an index then use { $set : { sublinks : X } } where X is the output of running sortArray("$sublinks","sublinkid")

If you can’t run the JS function the value for X in yoru case is below…

{
  "$cond": [{
      "$eq": [
        0,
        {
          "$size": "$sublinks"
        }
      ]
    },
    [],
    {
      "$slice": [{
          "$reduce": {
            "input": "$sublinks",
            "initialValue": [{
                "sublinkid": {
                  "$maxKey": 1
                }
              },
              {
                "sublinkid": {
                  "$minKey": 1
                }
              }
            ],
            "in": {
              "$let": {
                "vars": {
                  "rv": "$$value",
                  "rt": "$$this"
                },
                "in": {
                  "$let": {
                    "vars": {
                      "idx": {
                        "$reduce": {
                          "input": {
                            "$range": [
                              0,
                              {
                                "$size": "$$rv"
                              }
                            ]
                          },
                          "initialValue": 9999999,
                          "in": {
                            "$cond": [{
                                "$gt": [
                                  "$$rt.sublinkid",
                                  {
                                    "$arrayElemAt": [
                                      "$$rv.sublinkid",
                                      "$$this"
                                    ]
                                  }
                                ]
                              },
                              {
                                "$min": [
                                  "$$value",
                                  "$$this"
                                ]
                              },
                              "$$value"
                            ]
                          }
                        }
                      }
                    },
                    "in": {
                      "$concatArrays": [{
                          "$cond": [{
                              "$eq": [
                                0,
                                "$$idx"
                              ]
                            },
                            [],
                            {
                              "$slice": [
                                "$$rv",
                                0,
                                "$$idx"
                              ]
                            }
                          ]
                        },
                        [
                          "$$rt"
                        ],
                        {
                          "$slice": [
                            "$$rv",
                            "$$idx",
                            {
                              "$size": "$$rv"
                            }
                          ]
                        }
                      ]
                    }
                  }
                }
              }
            }
          }
        },
        1,
        {
          "$size": "$sublinks"
        }
      ]
    }
  ]
}
4 Likes