Details
-
Bug
-
Resolution: Works as Designed
-
Major - P3
-
None
-
None
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.