Why does the order of fields matter?
{"$pull": {"array_of_objects": {$in: [{"filed1":"value1", "filed2":"value2"}, ...]}}}
gives different result from:
{"$pull": {"array_of_objects": {$in: [{"filed2":"value2", "filed1":"value1"}, ...]}}}
Why does the order of fields matter?
{"$pull": {"array_of_objects": {$in: [{"filed1":"value1", "filed2":"value2"}, ...]}}}
gives different result from:
{"$pull": {"array_of_objects": {$in: [{"filed2":"value2", "filed1":"value1"}, ...]}}}
To have object equality the fields have to be in the same order with the same value.
I think it is like that for efficiency (and may be predictability) reasons.
The way is it right now is very simple to implement, you go field by field one by one and compare the value. To remove ordering would require to find each field of the rhs in the lhs with the same name before comparing the value. For 2 or 3 fields it would not be too expansive but any techniques would be of the order of 0( n2 ) rather than 0( n ).
Once you know that the best way is to use a well written API that always insert and update documents with the same order of fields.
thank you for the reply. But if replace $in with $or the query works as expected, field order does not matter any more. I am sure there is reason but it is not obvious and i want to know it.
I am pretty confident that if the queries you shared work by replacing $in with $or, it has a lot more to do with what you did not shared in place of the … rather than what you shared with filed1 and filed2. And most likely you meant to redact your real query with field1 and field2.