On this page
$concat (aggregation)
在本页面
Definition
$concat
- 连接字符串并返回连接的字符串。
$concat具有以下语法:
{ $concat: [ <expression1>, <expression2>, ... ] }
参数可以是任何有效的expression,只要它们解析为字符串即可。有关表达式的更多信息,请参见Expressions。
如果参数解析为null
的值或指向缺少的字段,则$concat返回null
。
Examples
考虑包含以下文档的inventory
集合:
{ "_id" : 1, "item" : "ABC1", quarter: "13Q1", "description" : "product 1" }
{ "_id" : 2, "item" : "ABC2", quarter: "13Q4", "description" : "product 2" }
{ "_id" : 3, "item" : "XYZ1", quarter: "14Q2", "description" : null }
以下操作使用$concat运算符使用“-”分隔符将item
字段和description
字段连接起来。
db.inventory.aggregate(
[
{ $project: { itemDescription: { $concat: [ "$item", " - ", "$description" ] } } }
]
)
该操作返回以下结果:
{ "_id" : 1, "itemDescription" : "ABC1 - product 1" }
{ "_id" : 2, "itemDescription" : "ABC2 - product 2" }
{ "_id" : 3, "itemDescription" : null }