$expr 演算子を使用すると、クエリ言語内で集計式を使用でき、同じドキュメントからのフィールドの比較、計算の実行、検索操作での集計演算子の使用が可能になります。
$expr 演算子は、従来のクエリ演算子では実現できない複雑なフィールドの比較に役立ちます。
構文
{
$expr: { <aggregation expression> }
}
パラメーター
| パラメーター | Description |
|---|---|
<aggregation expression> |
ブール値に評価される任意の有効な集計式。 この式には、フィールドの比較、算術演算、条件式、その他の集計演算子が含められます。 |
例示
stores コレクションのこのサンプル ドキュメントについて考えてみましょう。
{
"_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: フルタイムとパートタイムのスタッフを比較する
この例では、パートタイム従業員数が正社員より多い店舗を取得します。
db.stores.find({_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
$expr: {
$gt: ["$staff.employeeCount.partTime", "$staff.employeeCount.fullTime"]
}
})
クエリは、指定された (_id) ドキュメント内の 2 つのフィールドを比較し、条件が満たされた場合 (フルタイムスタッフ数がパートタイム スタッフ数を超える) 場合にのみ返します。
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"location": {
"lat": 70.1272,
"lon": 69.7296
},
"staff": {
"contractorCount": 5,
"employeeCount": { "fullTime": 19, "partTime": 20 }
},
"sales": {
"totalSales": 151864,
"salesByCategory": [
{
"categoryName": "Sound Bars",
"totalSales": 2120
},
{
"categoryName": "Home Theater Projectors",
"totalSales": 45004
}
},
"storeOpeningDate": ISODate("2024-09-23T13:45:01.480Z"),
"lastUpdated": ISODate("2025-06-11T11:06:57.922Z"),
"status": "active",
"category": "high-volume",
"priority": 1,
"reviewDate": ISODate("2025-06-11T11:10:50.276Z")
}
]
例 2: ストアの場所を指定した条件付きロジック
この例では、スタッフの効率率 (従業員あたりの売上) が 2000 を超える南半球の $expr プル ストアでの条件付きロジックの使用を示します。
db.stores.find({_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
$expr: {
$and: [
{ $gte: ["$location.lat", 70.1272] },
{
$gt: [
{
$divide: [
"$sales.totalSales",
{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }
]
},
2000
]
}
]
}
}).limit(1)
このクエリは、次の結果を返します。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"location": {
"lat": 70.1272,
"lon": 69.7296
},
"staff": {
"totalStaff": {
"fullTime": 19,
"partTime": 20
}
},
"sales": {
"totalSales": 151864,
"salesByCategory": [
{
"categoryName": "Sound Bars",
"totalSales": 2120
},
{
"categoryName": "Home Theater Projectors",
"totalSales": 45004
}
]
},
"storeOpeningDate": ISODate("2024-09-23T13:45:01.480Z"),
"lastUpdated": ISODate("2025-06-11T11:06:57.922Z"),
"status": "active",
"category": "high-volume",
"priority": 1,
"reviewDate": ISODate("2025-06-11T11:10:50.276Z")
}
]
関連コンテンツ
- MongoDB から Azure DocumentDB に移行するためのオプションを確認します。
- MongoDB との機能の互換性について詳しくは、こちらをご覧ください。