Lookup returns array unsorted, not respecting the original array's index order

This is the first time I notice a $lookup to an array return the matched documents in different order, like unsorted. And I have two questions…

Firstly, why’s that?

And secondly, I ended up with this solution. This is a pipeline inside a $lookup stage, where the ‘states’ variable is an array of ObjectId.

"pipeline": [
            {
                "$match": {
                    "$expr": {
                        "$in": ["$_id", "$states"]
                    }
                }
            },
            {
                "$addFields": {
                    "__order": {
                        "$indexOfArray": ["$states", "$_id"]
                    }
                }
            },
            {
                "$sort": {
                    "__order": 1
                }
            },
            {
                "$project": {
                    "__order": 0
                }
            }
        ],
        "as": "state"

I know that the $_id inside the $match stage is referring to the ‘foreign’ collection, so "$_id" "$in" "$states". Which is what I want.

But now. What does the second "$_id" mean? The one inside the "$addFields" stage.

Thank you for reading.

The second $_id is the same as in the $match.

It is not clear what $states is. With a single $ sign it is not a variable it is the value of the states field within the looked up collection, just like _id. If it is a variable defined with let: in the part of the code you did not share, then you have to use 2 $ signs, $$states rather than $states.

The only way to have a guarantied order is too $sort.

We can only help you further if you share the whole pipeline.