怎样做酒店网站ppt,全国企业查询系统,企业标识设计,东莞网站空间$concat
拼接字符串操作#xff0c;返回拼接后的字符串。语法格式如下#xff1a;
{ $concat: [ expression1, expression2, ... ] }参数可以是任何有效的表达式#xff0c;只要它们解析为字符串即可。 有关表达式的更多信息#xff0c;请参阅表达式。 示…$concat
拼接字符串操作返回拼接后的字符串。语法格式如下
{ $concat: [ expression1, expression2, ... ] }参数可以是任何有效的表达式只要它们解析为字符串即可。 有关表达式的更多信息请参阅表达式。 示例
准备以下测试数据
db.inventory.drop();
var rows
[{ _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 },{ _id : 4, item : CCCC, quarter: 4000}
];
db.inventory.insert(rows);使用 $conct 连接 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},
{ _id : 4, itemDescription : null}如果数据库中有不能解析成字符串的异常数据例如如下数据
{ _id : 5, item : CCCC, quarter: 4000, description : 4}则查询时会抛错误错误信息如下
{message : $concat only supports strings, not double,ok : 0,code : 16702,codeName : Location16702,name : MongoError
}总结
在使用 $concat 是做字符串拼接操作时如果参数解析为null值或引用缺少的字段则 $concat 返回null。 来自个人博客学习园 原文地址 https://xuexiyuan.cn/article/detail/222.html