运算符 $literal 返回指定值,而不将其分析为表达式。 如果需要在聚合管道中包含可能解释为字段名称、运算符或表达式的文本值,则此运算符非常有用。 它确保该值被视为常量,而不是被计算。
Syntax
{
$literal: <value>
}
参数
| 参数 | Description |
|---|---|
<value> |
任何应在不解释的情况下返回 as-is 的值。 可以是字符串、数字、布尔值、数组、对象或 null。 |
例子
请考虑商店集合中的这个示例文档。
{
"_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 值和布尔值
此查询演示如何使用 $literal null 值和布尔值,当需要显式设置这些值而不是计算这些值时尤其有用。
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
}
}
]
相关内容
- 查看用于 从 MongoDB 迁移到 Azure DocumentDB 的选项。
- 详细了解 与 MongoDB 的功能兼容性。