Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
L'operatore $bitNot esegue un'operazione NOT bit per bit sui valori integer. Inverte tutti i bit dell'operando, trasformando 1 in 0 e 0 in 1. Il risultato è il complemento bit per bit del valore di input.
Sintassi
{
$bitNot: <expression>
}
Parametri
| Parametro | Description |
|---|---|
expression |
Espressione che restituisce un numero intero. L'operatore $bitNot esegue un'operazione NOT bit per bit su questo valore. |
Esempi
Si consideri questo documento di esempio dalla raccolta negozi.
{
"_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
}
]
}
]
}
Esempio 1: Operazione NOT bit per bit di base
Questa query esegue un'inversione bit per bit nei campi del conteggio del personale per un documento di archivio specifico. I valori invertiti possono essere usati per operazioni speciali di flag di autorizzazione, interruttori di funzionalità o maschera di bit. Il NOT bit per bit di 14 risultati è -15 e il NOT bit per bit di 8 risultati in -9. Il risultato osservato è dovuto alla rappresentazione di complemento di due, dove ~n = -(n+1).
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"
}
}
}
])
Questa query restituisce il risultato seguente.
[
{
"_id": "26afb024-53c7-4e94-988c-5eede72277d5",
"name": "First Up Consultants | Microphone Bazaar - South Lexusland",
"fullTimeStaff": 14,
"partTimeStaff": 8,
"invertedFullTime": -15,
"invertedPartTime": -9
}
]
Esempio 2: Uso di $bitNot con percentuali di sconto
Questa query estrae ed elabora le informazioni sullo sconto per un archivio specifico e applica un'operazione NOT bit per bit per ogni percentuale di sconto. L'operazione NOT bit per bit inverte tutti i bit: 20 diventa -21 e 17 diventa -18.
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"
}
}
}
])
Questa query restituisce i risultati seguenti:
[
{
"_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
},
{
"_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
}
]