$비트

$bit 연산자는 정수 값에 대해 비트 연산을 수행하는 데 사용됩니다. 비트 AND, OR 및 XOR 작업을 적용하여 문서의 정수 필드를 업데이트하는 데 사용할 수 있습니다. $bit 같은 비트 연산자는 값을 증가시키는 것이 아니라 비트(예: 특정 비트 확인, 설정 또는 지우기)를 직접 조작하기 위해 설계되었습니다.

문법

{
    $bit: {
        < field >: {
            < operator >: < number >
        }
    }
}

매개 변수

매개 변수 Description
<field> 비트 연산을 수행할 필드입니다.
<operator> 수행할 비트 연산입니다. 다음 중 하나일 수 있습니다. andorxor
<number> 비트 연산에 사용할 숫자입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

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

예제 1: 에서 필드에 대해 partTime 비트 AND 연산 수행 totalStaff

db.stores.updateOne({
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
    $bit: {
        "staff.totalStaff.partTime": {
            and: 1
        }
    }
})

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "acknowledged": true,
    "insertedId": null,
    "matchedCount": "1",
    "modifiedCount": "1",
    "upsertedCount": 0
  }
]

예제 2: 에서 필드에 비트 partTime OR 연산 수행 totalStaff

db.stores.updateOne({
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
    $bit: {
        "staff.totalStaff.partTime": {
            "or": 1
        }
    }
})

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "acknowledged": true,
    "insertedId": null,
    "matchedCount": "1",
    "modifiedCount": "1",
    "upsertedCount": 0
  }
]

예제 3: 에서 필드에 대해 partTime 비트 XOR 연산 수행 totalStaff

db.stores.updateOne({
    _id: "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5"
}, {
    $bit: {
        "staff.totalStaff.partTime": {
            "xor": 1
        }
    }
})

이 쿼리는 다음 결과를 반환합니다.

[
  {
    "acknowledged": true,
    "insertedId": null,
    "matchedCount": "1",
    "modifiedCount": "1",
    "upsertedCount": 0
  }
]