$bitNot

El $bitNot operador realiza una operación NOT bit a bit en valores enteros. Invierte todos los bits del operando, convirtiendo 1s en 0 y 0 en 1s. El resultado es el complemento bit a bit del valor de entrada.

Syntax

{
  $bitNot: <expression>
}

Parámetros

Parámetro Description
expression Expresión que se evalúa como un entero. El $bitNot operador realiza una operación NOT bit a bit en este valor.

Examples

Considere este documento de ejemplo de la colección de tiendas.

{
    "_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
                }
            ]
        }
    ]
}

Ejemplo 1: Operación NOT bit a bit básica

Esta consulta realiza una inversión bit a bit en los campos de recuento de personal para un documento de almacén específico. Los valores invertidos se pueden usar para marcas de permisos especiales, alternancias de características o operaciones de máscara de bits. El VALOR NOT bit a bit de 14 resultados es -15 y el VALOR NOT bit a bit de 8 da como resultado -9. El resultado observado se debe a la representación complementaria de dos, donde ~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"
            }
        }
    }
])

Esta consulta devuelve el resultado siguiente.

[
  {
    "_id": "26afb024-53c7-4e94-988c-5eede72277d5",
    "name": "First Up Consultants | Microphone Bazaar - South Lexusland",
    "fullTimeStaff": 14,
    "partTimeStaff": 8,
    "invertedFullTime": -15,
    "invertedPartTime": -9
  }
]

Ejemplo 2: Uso de $bitNot con porcentajes de descuento

Esta consulta extrae y procesa información de descuento para un almacén específico y aplica una operación NOT bit a bit en cada porcentaje de descuento. La operación NOT bit a bit invierte todos los bits: 20 se convierte en -21 y 17 se convierte en -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"
            }
        }
    }
])

Esta consulta devuelve los siguientes resultados:

[
  {
    "_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
  }
]