I haven’t run a given PHP test against Atlas in over a month.
The order of objects in an $unwind on all documents in a collection is now the reverse of what it has been for years with respect to the order returned of the items in each unwound array. That is, if I have documents 1,2,3 … with array elements 1.1, 1,2, 2.1, 2.2, 3.1, 3.2 they used to come back in that order but now they are coming back 1.2, 1.1, 2.2, 2.1, 3.2, 3.1.
The order returned when I interactively do an $unwind on a single document in Compass is what I expect.
What has changed, please? Is this an artifact of the driver, or of MongoDB 8, or incipient madness on my part ?
the driver passes on the aggregation pipeline to the server and is not involved in the evaluation of it, so I’m guessing this would be a server change.
A quick test on my machine using the $documents stage as input did not reveal any differences between 7.0.9 and 8.0.3, but I may be missing something.
If you are relying on the correct order, I recommend using the includeArrayIndex option for $unwind to get the index of each element added to the document, then sorting on that for consistent ordering.
The behavior is as I expect if I expand the array from one document.
The behavior is as I have described if I try to unwind from all documents at once, with the $unwind stage the first stage in the pipeline: document order, but array order per document reversed.
Hmmm … I tried adding an empty $match stage before the $unwind
{ $match: {} }
In PHP, that becomes this (took me a moment to figure it out )
['$match' => [null => null]]
But the behavior is the same. I guess the behavior for unwinding all fields from all documents at once has changed and I’ll need to implement the solution suggested by @Andreas_Braun above.