-
Type: Improvement
-
Resolution: Unresolved
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
None
-
Query Optimization
It would be nice if there where specialized variants of the $eq operator for matching in an order-Independent fashion.
$eq-unordered could be used on arrays to implement sets:
> db.test.insert({_id: 1, colors: ["red", "blue"]}) > db.test.find({colors: {$eq-unordered: ["blue", "red"]}}) {_id: 1, colors: ["red", "blue"]})
For objects it would be useful, since json objects are usually treated as order-independent by most programming environment, so this would make subdocument matching easier.
Without this we have to do:
> db.test.insert({_id: 1, doc: {name: "Arthur", occupation: "Bulldozer-preventer"}}) > db.test.insert({_id: 2, doc: {name: "Arthur", occupation: "Bulldozer-preventer", do_not_ignore: "do not ignore this field"}}) > db.test.find({"doc.name": "Arthur", "doc.occupation": "Bulldozer-preventer"}) {_id: 1, doc: {name: "Arthur", occupation: "Bulldozer-preventer"}} {_id: 2, doc: {name: "Arthur", occupation: "Bulldozer-preventer", do_not_ignore: "do not ignore this field"}}
Note how the second document also is matched – I believe there is no syntax to prevent the match if there are extra fields so an exact order independant match is not possible.
With this we could do:
> db.test.find({doc: {$eq-unordered: {occupation: "Bulldozer-preventer", name: "Arthur"}}}) {_id: 1, doc: {name: "Arthur", occupation: "Bulldozer-preventer"}}
- related to
-
SERVER-5030 Document equality should be independent of field insertion order
- Backlog