$concat(聚合)
定义
例子
创建一个包含这些文档的 inventory
集合:
db.inventory.insertMany( [ { _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 }