$bitXor

L'operatore $bitXor esegue un'operazione OR (XOR) esclusiva bit per bit sui valori integer. L'operazione XOR restituisce 1 per ogni posizione di bit in cui i bit corrispondenti degli operandi sono diversi e 0 dove sono uguali.

Sintassi

{
  $bitXor: [ <expression1>, <expression2>, ... ]
}

Parametri

Parametro Description
expression1, expression2, ... Espressioni che si risolvono in valori interi. L'operatore esegue operazioni XOR su questi valori in sequenza.

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 XOR di base

Questa query usa una pipeline di aggregazione per calcolare il numero di personale a tempo pieno e part-time per un archivio specifico. Il documento risultante contiene i dettagli dell'archivio insieme a un campo calcolato. L'operazione XOR compresa tra 19 (binario: 10011) e 20 (binario: 10100) restituisce 7 (binario: 00111).

db.stores.aggregate([{
        $match: {
            _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
        }
    },
    {
        $project: {
            name: 1,
            fullTimeStaff: "$staff.totalStaff.fullTime",
            partTimeStaff: "$staff.totalStaff.partTime",
            staffXor: {
                $bitXor: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
            }
        }
    }
])

Questa query restituisce il risultato seguente.

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
    "fullTimeStaff": 19,
    "partTimeStaff": 20,
    "staffXor": 7
  }
]

Esempio 2: XOR con più valori

Questa query calcola l'XOR bit per bit di tutte le percentuali di sconto per l'evento Discount Delight Days di un archivio specifico. Il documento risultante rappresenta il calcolo XOR bit per bit di tutte le percentuali di sconto per l'evento Discount Delight Days .

db.stores.aggregate([{
        $match: {
            _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
        }
    },
    {
        $unwind: "$promotionEvents"
    },
    {
        $match: {
            "promotionEvents.eventName": "Discount Delight Days"
        }
    },
    {
        $unwind: "$promotionEvents.discounts"
    },
    {
        $group: {
            _id: "$_id",
            name: {
                $first: "$name"
            },
            eventName: {
                $first: "$promotionEvents.eventName"
            },
            discountPercentages: {
                $push: "$promotionEvents.discounts.discountPercentage"
            }
        }
    },
    {
        $project: {
            name: 1,
            eventName: 1,
            discountPercentages: 1,
            xorResult: {
                $reduce: {
                    input: {
                        $map: {
                            input: "$discountPercentages",
                            as: "val",
                            in: {
                                $toLong: "$$val"
                            }
                        }
                    },
                    initialValue: {
                        $toLong: 0
                    },
                    in: {
                        $bitXor: ["$$value", "$$this"]
                    }
                }
            }
        }
    }
])

Questa query restituisce il risultato seguente.

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
    "eventName": "Discount Delight Days",
    "discountPercentages": [22, 23, 10, 10, 9, 24],
    "xorResult": { "$numberLong": "16" }
  }
]