[DOCS-11536] Reverse byte positions match order of bitwise query by positions Created: 03/Apr/18  Updated: 30/Oct/23  Resolved: 03/Apr/18

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: None
Fix Version/s: Server_Docs_20231030

Type: Bug Priority: Major - P3
Reporter: Sin Assignee: Stennie Steneker (Inactive)
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:
Days since reply: 5 years, 45 weeks, 1 day ago

 Description   

According to the comments in source code, the steps of matching BinData by bitwise query is:

// Map to byte position and bit position within that byte. Note that byte positions
// start at position 0 in the char array, and bit positions start at the least
// significant bit.

That means, given the documents:

{ "_id" : 1, "a" : BinData(0, "AAAB"), "binaryValueofA" : "0000 0000 0000 0000 0000 0001" }

{ "_id" : 2, "a" : BinData(0, "AQAA"), "binaryValueofA" : "0000 0001 0000 0000 0000 0000" }

the query

db.collection.find({"a": {$bitsAnySet: [0]}})

matchs

{ "_id" : 2, "a" : BinData(0, "AQAA"), "binaryValueofA" : "0000 0001 0000 0000 0000 0000" }

and

db.collection.find({"a": {$bitsAnySet: [16]}})

matches

{ "_id" : 1, "a" : BinData(0, "AAAB"), "binaryValueofA" : "0000 0000 0000 0000 0000 0001" }

It's very counterintuitive and makes bitwise query operators much less useful. This behavior is also not clearly documented in the MongoDB Docs.
It should be better to keep the byte position order the same as the bit position order.



 Comments   
Comment by Sin [ 04/Apr/18 ]

The document only explains the behavior within one byte, but not clearly explains the situation of multiple bytes. E.g., according to the document, we expect the positions like this:

Bit Value 1 1 1 1 1 1 1 1 0
Position 8 7 6 5 4 3 2 1 0

However, the actual behavior is:

Bit Value 1 1 1 1 1 1 1 1 0
Position 0 15 14 13 12 11 10 9 8

Comment by Stennie Steneker (Inactive) [ 03/Apr/18 ]

Since BSON types must be serialized in little endian format, the least significant bit comparison matches the BSON representation and ensures consistent cross-platform comparison.

This behaviour is documented for the $bitsAnySet query operator:

Position List
If querying a list of bit positions, each <position> must be a non-negative integer. Bit positions start at 0 from the least significant bit. For example, the decimal number 254 would have the following bit positions:

Regards,
Stennie

Generated at Thu Feb 08 08:03:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.