Set operators, and order of elements


Can i trust the order of elements after set operators?

The bellow example union arrays, and makes comparisons after, with not-expected results.

How the comparison is done?

  • they are compared as sets?
  • they are converted to sorted arrays? (looks like the second is happening)

The results are not expected because from the manual for $setUnion for example it says that order is not specified, but seems like it is specified, at least in MongoDB 5 that i use.

The first compares range 100000 with range 100000 reversed , after the union,
and returns true.

       [{"$setUnion": [{"$range": [0, 100000]}, [10]]},
          [{"$reverseArray": {"$range": [0, 100000]}}, [10]]}]}}},
  {"$set": {"b": {"$setUnion": [{"$range": [0, 5]}, []]}}},
    {"c": {"$setUnion":
             [{"$reverseArray": {"$range": [0, 5]}}, []]}}}])


  "_id": ObjectId("61893adcf809202ae070d224"),
  "a": true,
  "b": [0,1,2,3,4],
  "c": [0,1,2,3,4]