Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
APPLIES TO:
MongoDB vCore
The $bitNot
operator performs a bitwise NOT operation on integer values. It inverts all the bits of the operand, turning 1s into 0s and 0s into 1s. The result is the bitwise complement of the input value.
Syntax
The syntax for the $bitNot
operator is as follows:
{
$bitNot: <expression>
}
Parameters
Description | |
---|---|
expression |
An expression that evaluates to an integer. The $bitNot operator performs a bitwise NOT operation on this value. |
Example
Let's understand the usage with sample json from stores
dataset.
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"location": {
"lat": -29.1866,
"lon": -112.7858
},
"staff": {
"totalStaff": {
"fullTime": 14,
"partTime": 8
}
},
"sales": {
"totalSales": 83865,
"salesByCategory": [
{
"categoryName": "Lavalier Microphones",
"totalSales": 44174
},
{
"categoryName": "Wireless Microphones",
"totalSales": 39691
}
]
},
"promotionEvents": [
{
"eventName": "Incredible Savings Showcase",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Condenser Microphones",
"discountPercentage": 20
},
{
"categoryName": "Microphone Stands",
"discountPercentage": 17
}
]
}
]
}
Example 1: Basic bitwise NOT operation
Perform a bitwise NOT operation on staff numbers to create inverted flags.
db.stores.aggregate([
{ $match: {"_id": "26afb024-53c7-4e94-988c-5eede72277d5"} },
{
$project: {
name: 1,
fullTimeStaff: "$staff.totalStaff.fullTime",
partTimeStaff: "$staff.totalStaff.partTime",
invertedFullTime: {
$bitNot: "$staff.totalStaff.fullTime"
},
invertedPartTime: {
$bitNot: "$staff.totalStaff.partTime"
}
}
}
])
This produces the following output:
[
{
_id: '26afb024-53c7-4e94-988c-5eede72277d5',
name: 'First Up Consultants | Microphone Bazaar - South Lexusland',
fullTimeStaff: 14,
partTimeStaff: 8,
invertedFullTime: -15,
invertedPartTime: -9
}
]
The bitwise NOT of 14 results in -15, and the bitwise NOT of 8 results in -9. This is due to two's complement representation where ~n = -(n+1).
Example 2: Using $bitNot with Discount Percentages
Apply bitwise NOT operations on discount percentages.
db.stores.aggregate([
{ $match: {"_id": "26afb024-53c7-4e94-988c-5eede72277d5"} },
{ $unwind: "$promotionEvents" },
{ $match: {"promotionEvents.eventName": "Incredible Savings Showcase"} },
{ $unwind: "$promotionEvents.discounts" },
{
$project: {
name: 1,
eventName: "$promotionEvents.eventName",
categoryName: "$promotionEvents.discounts.categoryName",
discountPercentage: "$promotionEvents.discounts.discountPercentage",
invertedDiscount: {
$bitNot: "$promotionEvents.discounts.discountPercentage"
}
}
}
])
This produces the following output:
[
{
_id: '26afb024-53c7-4e94-988c-5eede72277d5',
name: 'First Up Consultants | Microphone Bazaar - South Lexusland',
eventName: 'Incredible Savings Showcase',
categoryName: 'Condenser Microphones',
discountPercentage: 20,
invertedDiscount: -21
},
{
_id: '26afb024-53c7-4e94-988c-5eede72277d5',
name: 'First Up Consultants | Microphone Bazaar - South Lexusland',
eventName: 'Incredible Savings Showcase',
categoryName: 'Microphone Stands',
discountPercentage: 17,
invertedDiscount: -18
}
]
The bitwise NOT operation inverts all bits: 20 becomes -21 and 17 becomes -18.
Related content
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB