How to use bitwise operators in MongoDB to filter data
Bitwise operators
Bitwise operators are used to run query conditions based on the location of bits. MongoDB provides four types of bitwise operators to filter data.
$bitsAllClear$bitsAllSet$bitsAnyClear$bitsAnySet
We use the following database to perform different bitwise operations:
use db //selecting our databasedb.dummy.find() //showing all documents[{ _id: 1, var: 9, binaryValue: '1001' },{ _id: 2, var: 10, binaryValue: '1010' },{ _id: 3, var: 11, binaryValue: '1011' },{ _id: 4, var: 12, binaryValue: '1100' },{ _id: 5, var: 13, binaryValue: '1101' }]
Note: Remember that we read bit position from right to left. The right most position is
0.
$bitsAllClear
It matches documents where all the locations of bits specified in the query are clear—0.
Syntax
The syntax of the $bitsAllClear for numeric bitmask is as follows:
{<field>: {$bitsAllClear: <numeric bitmask>}}
The syntax of the $bitsAllClear for BinData bitmask is as follows:
{<field>: {$bitsAllClear: < BinData bitmask>}}
Parameters
The description of the above-mentioned parameters is given below:
field: A unique identifier for storing data values.BinData: It is a type used for binary data.BSON BSON stands for Binary Javascript Object Notation. It is a binary-encoded representation of JSON documents. bitmask: It defines which bytes we require.
Example
Here's an example of $bitsAllClear :
//querydb.dummy.find( { var: { $bitsAllClear: [ 1, 2 ] } } )//output[{ _id: 1, var: 9, binaryValue: ‘1001’ }]
Note: Remeber that set means bit value is 1 and clear means bit value is 0.
$bitsAllSet
It matches documents where all the locations of bits specified in the query are set—1.
Syntax
The syntax of the $bitsAllSet for numeric bitmask is as follows:
{<field>: {$bitsAllSet: <numeric bitmask>}}
The syntax of the $bitsAllSet for BinData bitmask is as follows:
{<field>: {$bitsAllSet: < BinData bitmask>}}
Example
Here's an example of $bitsAllSet :
//querydb.dummy.find({ var: { $bitsAllSet: [2,3] } })//output[{ _id: 4, var: 12, binaryValue: ‘1100’ },{ _id: 5, var: 13, binaryValue: ‘1101’ }]
$bitsAnyClear
It matches documents where any location of bits specified in the query are clear—0.
Syntax
The syntax of the $bitsAnyClear for numeric bitmask is as follows:
{<field>: {$bitsAnyClear: <numeric bitmask>}}
The syntax of the $bitsAnyClear for BinData bitmask is as follows:
{<field>: {$bitsAnyClear: < BinData bitmask>}}
Example
Here's an example of $bitsAnyClear:
//querydb.dummy.find( { var: { $bitsAnyClear: [ 3, 2 ] } } )//output[{ _id: 1, var: 9, binaryValue: '1001' },{ _id: 2, var: 10, binaryValue: '1010' },{ _id: 3, var: 11, binaryValue: '1011' }]
$bitsAnySet
It matches documents where any location of bits specified in the query are set—1.
Syntax
The syntax of the $bitsAnySet for numeric bitmask is as follows:
{<field>: {$bitsAnySet: <numeric bitmask>}}
The syntax of the $bitsAnySet for BinData bitmask is as follows:
{<field>: {$bitsAnySet: < BinData bitmask>}}
Example
Here's an example of $bitsAnySet:
//querydb.dummy.find( { var: { $bitsAnySet: [ 0, 2 ] } } )//output[{ _id: 1, var: 9, binaryValue: '1001' },{ _id: 3, var: 11, binaryValue: '1011' },{ _id: 4, var: 12, binaryValue: '1100' },{ _id: 5, var: 13, binaryValue: '1101' }]
Run your queries
You can run all the previous MongoDB queries in the terminal below:
Free Resources