$bitNot

$bitNot işleci, tamsayı değerlerinde bit düzeyinde DEĞİl işlemi gerçekleştirir. İşlenenin tüm bitlerini tersine çevirerek 1'leri 0'lara, 0'ları 1'e dönüştürür. Sonuç, giriş değerinin bit düzeyinde tamamlayıcısı olur.

Sözdizimi

{
  $bitNot: <expression>
}

Parametreler

Parametre Description
expression Tamsayı olarak değerlendirilen ifade. işleci $bitNot bu değer üzerinde bit düzeyinde DEĞİl işlemi gerçekleştirir.

Örnekler

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

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

Örnek 1: Temel bit düzeyinde DEĞİl işlemi

Bu sorgu, belirli bir mağaza belgesi için personel sayısı alanlarında bit düzeyinde ters işlem gerçekleştirir. Ters değerler özel izin bayrakları, özellik geçişleri veya bit maskesi işlemleri için kullanılabilir. 14 sonucun bit tabanlı DEĞİl'i -15, bit tabanlı NOT ise -9 olur. Gözlemlenen sonuç, ~n = -(n+1) olan iki tamamlayıcı gösteriminden kaynaklanır.

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"
            }
        }
    }
])

Bu sorgu aşağıdaki sonucu döndürür.

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

Örnek 2: İndirim yüzdeleriyle $bitNot kullanma

Bu sorgu belirli bir mağaza için indirim bilgilerini ayıklar ve işler ve her indirim yüzdesine bit düzeyinde DEĞİl işlemi uygular. Bit düzeyinde DEĞİl işlemi tüm bitleri ters çevirir: 20 -21, 17 ise -18 olur.

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"
            }
        }
    }
])

Bu sorgu aşağıdaki sonuçları döndürür:

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