该 $bitAnd 运算符对整数值执行 bitwise AND 运算。 它将第一个作数的每一位与第二个作数的对应位进行比较。 如果两个位均为 1,则对应的结果位将设置为 1。 否则,将对应的结果位设置为 0。
Syntax
{
$bitAnd: [ <expression1>, <expression2>, ... ]
}
参数
| 参数 | Description |
|---|---|
expression1, expression2, ... |
计算结果为整数的表达式。 该 $bitAnd 运算符对所有提供的表达式执行按位 AND 运算。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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:基本按位 AND作
此查询检索特定存储的员工信息,并在全职员工和兼职员工数之间计算 bitwise AND 以创建权限标志。
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$project: {
name: 1,
fullTimeStaff: "$staff.totalStaff.fullTime",
partTimeStaff: "$staff.totalStaff.partTime",
staffPermissionFlag: {
$bitAnd: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"]
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"fullTimeStaff": 19,
"partTimeStaff": 20,
"staffPermissionFlag": 16
}
]
示例 2:多个值 $bitAnd
此查询基于单个存储的多个数值字段检查按位权限或组合标志。
db.stores.aggregate([{
$match: {
_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"
}
},
{
$project: {
name: 1,
combinedFlag: {
$bitAnd: [
"$staff.totalStaff.fullTime",
"$staff.totalStaff.partTime",
255
]
}
}
}
])
此查询返回以下结果。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"combinedFlag": 16
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。