[SERVER-51258] Unexpected behavior in range query on whole embedded documents with MinKey and MaxKey subfield values Created: 01/Oct/20 Updated: 23/Jan/21 Resolved: 23/Jan/21 |
|
| Status: | Closed |
| Project: | Core Server |
| Component/s: | Index Maintenance, Querying |
| Affects Version/s: | 4.2.9 |
| Fix Version/s: | None |
| Type: | Question | Priority: | Major - P3 |
| Reporter: | Scott Crunkleton | Assignee: | Geert Bosch |
| Resolution: | Duplicate | Votes: | 0 |
| Labels: | None | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Issue Links: |
|
||||||||||||||||||||
| Sprint: | Execution Team 2020-11-16, Execution Team 2020-12-14, Execution Team 2020-12-28, Execution Team 2021-01-11, Execution Team 2021-01-25 | ||||||||||||||||||||
| Participants: | |||||||||||||||||||||
| Description |
|
According to https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#objects, embedded objects/documents are compared, first, by field names, second, by field values.
Consider a collection:
and a query:
I thought this would return ONLY doc 3 (since `bab` and `bar` are less than `baz`), but it returns ALL three docs. Changing the query to
Is this an issue? Are range queries supported on whole embedded objects values? Do MinKey and MaxKey have some weird behavior in queries embedded documents? |
| Comments |
| Comment by Geert Bosch [ 23/Jan/21 ] | ||||||||||||
|
The documentation has been fixed. | ||||||||||||
| Comment by Geert Bosch [ 01/Dec/20 ] | ||||||||||||
|
I can confirm this is an issue in the documentation. Value types are compared before key field names. | ||||||||||||
| Comment by Scott Crunkleton [ 07/Oct/20 ] | ||||||||||||
|
Thanks, Eric! | ||||||||||||
| Comment by Eric Sedor [ 06/Oct/20 ] | ||||||||||||
|
Thanks crunk1@gmail.com, I see what you are saying about the ambiguity in the docs. I'm transferring your bsonWoCompare output from
These results are all consistent with the type-specific ordering described at https://docs.mongodb.com/manual/reference/bson-type-comparison-order/, but the initial "When comparing values" language does strongly imply in the context of the Objects section that this comparison will occur after field names are compared. I'm passing this on to an appropriate team to clarify. | ||||||||||||
| Comment by Scott Crunkleton [ 05/Oct/20 ] | ||||||||||||
|
Isn't "documented type comparison order" only applicable in comparing field values (from object comparison point #3)? Field names (object comparison point #2) are always strings. So why is (bab, 123) > (baz, MinKey())? Shouldn't "bab" and "baz" be compared BEFORE 123 and MinKey()? | ||||||||||||
| Comment by Eric Sedor [ 05/Oct/20 ] | ||||||||||||
|
Items are compared using the documented type comparison order at the top of the page, with the MinKey type sorting before Numbers, which sort before Symbols/Strings. Since we haven't identified a bug in this behavior, I'm going to close this ticket. For further assistance troubleshooting, I encourage you to ask our community by posting on the MongoDB Developer Community Forums. | ||||||||||||
| Comment by Scott Crunkleton [ 01/Oct/20 ] | ||||||||||||
|
Just to have it here, the documentation says:
So you're saying that, per item 1, the key-value pairs (bab, 123) and (bar, "BAR") are greater than (baz, MinKey())? How are key-value pairs compared? Comparing these as tuples in Python, for example, would still show (baz, MinKey()) as greater. | ||||||||||||
| Comment by Eric Sedor [ 01/Oct/20 ] | ||||||||||||
|
It looks like this is working as expected. Item 1 in the Objects section is still within the context of the documented type comparison order at the top of the page. So a key/value pair where the value is of type MinKey will be less than a key/value pair where the value is of type string, regardless of the key name. Does this make sense? | ||||||||||||
| Comment by Eric Sedor [ 01/Oct/20 ] | ||||||||||||
|
I've edited the description crunk1@gmail.com, thank you. We are taking a look. | ||||||||||||
| Comment by Scott Crunkleton [ 01/Oct/20 ] | ||||||||||||
|
Someone mind editing the description for me: I thought this would return ONLY doc 3 (since `bab` and `bar` are less than `baz`), but it returns ALL three docs. Changing the query to
|