$literal演算子は、式として解析せずに、指定した値を返します。 この演算子は、集計パイプラインにフィールド名、演算子、または式として解釈される可能性があるリテラル値を含める必要がある場合に便利です。 これにより、値が評価されるのではなく、定数として扱われることが保証されます。
構文
{
$literal: <value>
}
パラメーター
| パラメーター | Description |
|---|---|
<value> |
解釈せずに as-is 返される必要がある値。 文字列、数値、ブール値、配列、オブジェクト、または null を指定できます。 |
例示
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.aggregate([
{ $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
{
$project: {
name: 1,
totalSales: "$sales.salesByCategory.totalSales",
status: { $literal: "Active" },
reviewStatus: { $literal: "$pending" },
priority: { $literal: 1 },
metadata: {
$literal: {
lastUpdated: "2024-06-13",
source: "automated-system"
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"totalSales": 37701,
"status": "Active",
"reviewStatus": "$pending",
"priority": 1,
"metadata": {
"lastUpdated": "2024-06-13",
"source": "automated-system"
}
}
]
例 2: リテラル配列と条件値の作成
このクエリでは、配列と条件式で $literal を使用して、値が演算子として解釈されないようにする方法を示します。
db.stores.aggregate([
{ $match: {"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
{
$project: {
name: 1,
totalSales: "$sales.salesByCategory.totalSales",
performanceCategory: {
$cond: {
if: { $gte: ["$sales.totalSales", 100000] },
then: { $literal: "High Performer" },
else: { $literal: "Standard" }
}
},
tags: { $literal: ["$featured", "$promoted", "$new"] },
searchKeywords: {
$literal: {
"$or": ["electronics", "entertainment"],
"$and": ["home", "theater"]
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
"name": "Proseware, Inc. | Home Entertainment Hub - East Linwoodbury",
"totalSales": 160000,
"performanceCategory": "High Performer",
"tags": ["$featured", "$promoted", "$new"],
"searchKeywords": {
"$or": ["electronics", "entertainment"],
"$and": ["home", "theater"]
}
}
]
例 3: リテラル null 値とブール値
このクエリは、null 値とブール値で $literal を使用する方法を示しています。特に、これらの値を計算するのではなく明示的に設定する必要がある場合に便利です。
db.stores.aggregate([
{ $match: {"_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6"} },
{
$project: {
name: 1,
totalSales: "$sales.salesByCategory.totalSales",
specialOffer: { $literal: null },
isOnline: { $literal: false },
hasInventory: { $literal: true },
calculationFormula: { $literal: "$multiply: [price, quantity]" },
ratingSystem: {
$literal: {
min: 0,
max: 5,
default: null
}
}
}
}
])
このクエリは、次の結果を返します。
[
{
"_id": "e6895a31-a5cd-4103-8889-3b95a864e5a6",
"name": "VanArsdel, Ltd. | Picture Frame Store - Port Clevelandton",
"totalSales": 17676,
"specialOffer": null,
"isOnline": false,
"hasInventory": true,
"calculationFormula": "$multiply: [price, quantity]",
"ratingSystem": {
"min": 0,
"max": 5,
"default": null
}
}
]