该 $and
运算符对表达式数组执行逻辑 AND 运算,并检索满足所有表达式的文档。
语法
{
$and: [{
< expression1 >
}, {
< expression2 >
}, ..., {
< expressionN >
}]
}
参数
参数 | 说明 |
---|---|
expression |
结果中包含文档必须全部为 true 的表达式数组 |
例子
让我们了解 stores
数据集中的示例 json 的用法。
{
"_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 运算符作为逻辑查询
查询筛选了使用操作员的全职员工数大于 10 且兼职员工小于 15 $and
的商店。 它仅 name
投影和字段, staff
并将结果限制为三条记录。
db.stores.find({
$and: [{
"staff.employeeCount.fullTime": {
$gt: 10
}
}, {
"staff.employeeCount.partTime": {
$lt: 15
}
}]
}, {
"name": 1,
"staff": 1
}).limit(3)
该查询最多返回三家商店,其中包含高全职但有限的兼职人员配备。
[
{
"_id": "e60c807b-d31c-4903-befb-5d608f260ba3",
"name": "Wide World Importers | Appliance Emporium - Craigfort",
"staff": {
"totalStaff": {
"fullTime": 11,
"partTime": 8
}
}
},
{
"_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7",
"name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph",
"staff": {
"totalStaff": {
"fullTime": 14,
"partTime": 0
}
}
},
{
"_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
"name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
"staff": {
"totalStaff": {
"fullTime": 16,
"partTime": 8
}
}
}
]
示例 2:使用 AND 运算符作为布尔表达式来查找销售额高且员工足够多的商店
该示例查找总销售额超过 100,000 和 30 多名员工总数的商店。
db.stores.aggregate([
{
$project: {
name: 1,
totalSales: "$sales.totalSales",
totalStaff: {
$add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"]
},
meetsHighPerformanceCriteria: {
$and: [
{ $gt: ["$sales.totalSales", 100000] },
{ $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] }
]
}
}
},
{ $limit: 2 }
])
该查询返回满足高销售额和人员配备条件的商店。
{
"_id": "905d1939-e03a-413e-a9c4-221f74055aac",
"name": "Trey Research | Home Office Depot - Lake Freeda",
"totalStaff": 31,
"meetsHighPerformanceCriteria": false
},
{
"_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
"name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
"totalStaff": 27,
"meetsHighPerformanceCriteria": false
}