Assume I have a BinData field of arbitrary length that stores n-byte items. The offset of each item is known before the query and all items are n bytes. I want to test an item at offset X with bitmask Y. It is important to mention that I don't want to do a search, just a bitmask test, for the offset is known.

Currently, I'm reading the whole field and testing on my server side.

Is there a way to do it on MongoDB side?

1 Answer 1


From what I see in the question, it still needs clarification about why you need to point out the offset as well because I assume you can directly point out to the bitmask position as reflected below:

The following query uses the $bitsAllSet operator to test whether field a has bits set at positions 4 and 5 (the binary representation of BinData(0, "MA==") is 00110000).

db.collection.find( { a: { $bitsAllSet: BinData(0, "MA==") } } )

The query matches the following document:

{ _id: 1, a: 54, binaryValueofA: "00110110" }

Source: MongoDB documentation

  • Thanks for your comment! The binary data can be much larger than a 32-bit mask. Say I have a 108 bytes array buffer, and I want to check the 84-th byte... I still have a mask of 1 byte, but the offset would be 83... Commented Apr 23 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.