Nota
O acesso a esta página requer autorização. Pode tentar iniciar sessão ou alterar os diretórios.
O acesso a esta página requer autorização. Pode tentar alterar os diretórios.
O $bit operador é usado para executar operações bit a bit em valores inteiros. Ele pode ser usado para atualizar campos inteiros em documentos aplicando operações bit a bit AND, OR e XOR. Operadores bitwise como $bit não são projetados para incrementar valores, mas para manipular bits diretamente (como verificar, definir ou limpar bits específicos).
Sintaxe
{
$bit: {
< field >: {
< operator >: < number >
}
}
}
Parâmetros
| Parâmetro | Description |
|---|---|
<field> |
O campo para executar a operação bitwise. |
<operator> |
A operação bit a ser executada. Pode ser um dos: and, or, xor. |
<number> |
O número a ser usado para a operação bitwise. |
Examples
Considere este documento de exemplo da coleção de lojas.
{
"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
"name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
"location": {
"lat": -89.2384,
"lon": -46.4012
},
"staff": {
"totalStaff": {
"fullTime": 8,
"partTime": 20
}
},
"sales": {
"totalSales": 75670,
"salesByCategory": [
{
"categoryName": "Wine Accessories",
"totalSales": 34440
},
{
"categoryName": "Bitters",
"totalSales": 39496
},
{
"categoryName": "Rum",
"totalSales": 1734
}
]
},
"promotionEvents": [
{
"eventName": "Unbeatable Bargain Bash",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 6,
"Day": 23
},
"endDate": {
"Year": 2024,
"Month": 7,
"Day": 2
}
},
"discounts": [
{
"categoryName": "Whiskey",
"discountPercentage": 7
},
{
"categoryName": "Bitters",
"discountPercentage": 15
},
{
"categoryName": "Brandy",
"discountPercentage": 8
},
{
"categoryName": "Sports Drinks",
"discountPercentage": 22
},
{
"categoryName": "Vodka",
"discountPercentage": 19
}
]
},
{
"eventName": "Steal of a Deal Days",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 29
}
},
"discounts": [
{
"categoryName": "Organic Wine",
"discountPercentage": 19
},
{
"categoryName": "White Wine",
"discountPercentage": 20
},
{
"categoryName": "Sparkling Wine",
"discountPercentage": 19
},
{
"categoryName": "Whiskey",
"discountPercentage": 17
},
{
"categoryName": "Vodka",
"discountPercentage": 23
}
]
}
]
}
Exemplo 1: Executar uma operação bit a partTime bit E no campo em totalStaff
db.stores.updateOne({
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
$bit: {
"staff.totalStaff.partTime": {
and: 1
}
}
})
Esta consulta retorna o seguinte resultado.
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
Exemplo 2: Executar uma operação OR bit a bit no partTime campo em totalStaff
db.stores.updateOne({
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
$bit: {
"staff.totalStaff.partTime": {
"or": 1
}
}
})
Esta consulta retorna o seguinte resultado.
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]
Exemplo 3: Executar uma operação XOR bit a bit no partTime campo em totalStaff
db.stores.updateOne({
_id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
$bit: {
"staff.totalStaff.partTime": {
"xor": 1
}
}
})
Esta consulta retorna o seguinte resultado.
[
{
"acknowledged": true,
"insertedId": null,
"matchedCount": "1",
"modifiedCount": "1",
"upsertedCount": 0
}
]