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.
The $bit
operator is used to perform bitwise operations on integer values. It can be used to update integer fields in documents by applying bitwise AND, OR, and XOR operations. Bitwise operators like $bit aren't designed for incrementing values, but for manipulating bits directly (like checking, setting, or clearing specific bits).
{ $bit: { <field>: { <operator>: <number> } } }
Description | |
---|---|
<field> |
The field to perform the bitwise operation on. |
<operator> |
The bitwise operation to perform. Can be one of: and , or , xor . |
<number> |
The number to use for the bitwise operation. |
Let's understand the usage with the following sample json.
{
"_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
"name": "Lakeshore Retail | DJ Equipment Stop - Port Cecile",
"location": {
"lat": 60.1441,
"lon": -141.5012
},
"staff": {
"totalStaff": {
"fullTime": 2,
"partTime": 0
}
},
"sales": {
"salesByCategory": [
{
"categoryName": "DJ Headphones",
"totalSales": 35921
}
],
"fullSales": 3700
},
"promotionEvents": [
{
"eventName": "Bargain Blitz Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 3,
"Day": 11
},
"endDate": {
"Year": 2024,
"Month": 2,
"Day": 18
}
},
"discounts": [
{
"categoryName": "DJ Turntables",
"discountPercentage": 18
},
{
"categoryName": "DJ Mixers",
"discountPercentage": 15
}
]
}
],
"tag": [
"#ShopLocal",
"#SeasonalSale",
"#FreeShipping",
"#MembershipDeals"
]
}
db.stores.updateOne(
{ "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" },
{ $bit: { "staff.totalStaff.partTime": { and: 1 } } }
)
This query would return the following document. The AND of 0 and 1 is 0 hence the new value of the field is 0.
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
db.stores.updateOne(
{ "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" },
{ $bit: { "staff.totalStaff.partTime": { "or" : 1 } } }
)
This query would return the following document. The OR of 0 and 1 is 1 hence the new value of the field is 1.
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
db.stores.updateOne(
{ "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5" },
{ $bit: { "staff.totalStaff.partTime": { "xor" : 1 } } }
)
This query would return the following document. The XOR of 1 and 1 is 0 hence the new value of the field is 0.
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB