Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11536

Reverse byte positions match order of bitwise query by positions

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Works as Designed
    • Icon: Major - P3 Major - P3
    • Server_Docs_20231030
    • None
    • manual
    • 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.

      Attachments

        Activity

          People

            stephen.steneker@mongodb.com Stennie Steneker (Inactive)
            sin Sin
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              5 years, 45 weeks, 1 day ago