适用对象:
MongoDB vCore
运算符 $literal
返回指定值,而不将其分析为表达式。 如果需要在聚合管道中包含可能解释为字段名称、运算符或表达式的文本值,则此运算符非常有用。 它确保该值被视为常量,而不是被计算。
语法
$literal
运算符的语法如下:
{
$literal: <value>
}
参数
DESCRIPTION | |
---|---|
<value> |
任何应在不解释的情况下返回 as-is 的值。 可以是字符串、数字、布尔值、数组、对象或 null。 |
示例:
让我们了解 stores
数据集中的示例 json 的用法。
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"location": {
"lat": 60.7954,
"lon": -142.0012
},
"staff": {
"totalStaff": {
"fullTime": 18,
"partTime": 17
}
},
"sales": {
"totalSales": 37701,
"salesByCategory": [
{
"categoryName": "Mattress Toppers",
"totalSales": 37701
}
]
},
"promotionEvents": [
{
"eventName": "Price Drop Palooza",
"promotionalDates": {
"startDate": {
"Year": 2024,
"Month": 9,
"Day": 21
},
"endDate": {
"Year": 2024,
"Month": 9,
"Day": 30
}
},
"discounts": [
{
"categoryName": "Bath Accessories",
"discountPercentage": 18
},
{
"categoryName": "Pillow Top Mattresses",
"discountPercentage": 17
}
]
}
]
}
示例 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"
}
}
}
}
])
查询将文本值添加到每个存储文档。 请注意如何 $pending
被视为文本字符串而不是字段引用。
{
"_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
}
}
}
}
])
该查询添加显式文本值,包括 null、布尔值和类似于 MongoDB 表达式的字符串。
{
"_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
}
}