MongoDB 聚合管道中使用字符串表达式运算符

字符串表达式运算符主要用于实现字符串操作,主要包括了大小写转换、字符串截取、拼接、替换等

一、准备工作

初始化字符串数据

db.strings.insertMany([
    { "_id": "1", "comment": " Abc" },
    { "_id": "2", "comment": "Hello World" },
    { "_id": "3", "comment": " Hello World " }
])

二、去字符串($ltrim,$rtrim,$trim)

语法:

        去除开始位置的字符串:{ $ltrim: { input: <string>,  chars: <string> } }

        去除结束位置的字符串:{ $rtrim: { input: <string>,  chars: <string> } }

        去除开始和结束位置的字符串:{ $trim: { input: <string>,  chars: <string> } }

其中,

        input:代表的是需要去除字符的字符串

        chars:可选,代表的是需要去除的字符串;如果未定义,则去除空格

例子:去除开始位置的Hello

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $ltrim: {
                    input: "$comment",
                    chars: "Hello"
                }
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " Abc" }
{ "_id" : "2", "comment" : " World" }
{ "_id" : "3", "comment" : " Hello World " }

可以看到,编号为2的数据中的Hello去掉了,编号为3的未去掉,原因是$ltrim是去除开始位置的字符串,而编号为3的数据的开始位置为空格

例子:去除结束位置的空格

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $rtrim: {
                    input: "$comment"
                }
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " Abc" }
{ "_id" : "2", "comment" : "Hello World" }
{ "_id" : "3", "comment" : " Hello World" }

例子:去除开始和结束位置的空格

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $trim: {
                    input: "$comment"
                }
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : "Abc" }
{ "_id" : "2", "comment" : "Hello World" }
{ "_id" : "3", "comment" : "Hello World" }

可以看出,$trim只是去除开始和结束位置的字符串

需要注意的是,$lrim,$rtrim,$trim只能在4.0及之后的版本才能使用

三、拼接($concat)

语法:{ $concat: [ <expression1>, <expression2>, ... ] }

将多个表达式的结果拼接到一起

例子:将编号和comment拼接到一起,后面再拼接上ok

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $concat: [ "$_id", "$comment", "ok" ]
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : "1 Abcok" }
{ "_id" : "2", "comment" : "2Hello Worldok" }
{ "_id" : "3", "comment" : "3 Hello World ok" }

四、分割($split)

语法:{ $split: [ <string expression>, <delimiter> ] }

使用分隔符将字符串分割成字符串数组

其中,

        <string expression>:指的是待分割的字符串表达式

        <delimiter>:指的是字符串分隔符

例子:去除两边空格后使用空格分割comment

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $split: [ 
                    { $trim: { input: "$comment" } }, 
                    " " 
                ]
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : [ "Abc" ] }
{ "_id" : "2", "comment" : [ "Hello", "World" ] }
{ "_id" : "3", "comment" : [ "Hello", "World" ] }

五、转大写($toUpper)、转小写($toLower)

语法:

        转大写:{ $toUpper: <expression> }

        转小写:{ $toLower: <expression> }

例子:转换commet为大写

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $toUpper: "$comment"
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " ABC" }
{ "_id" : "2", "comment" : "HELLO WORLD" }
{ "_id" : "3", "comment" : " HELLO WORLD " }

例子:转换commet为小写

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $toLower: "$comment"
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " abc" }
{ "_id" : "2", "comment" : "hello world" }
{ "_id" : "3", "comment" : " hello world " }

六、替换($replaceOne,$replaceAll

语法:

        替换一个:{ $replaceOne: { input: <expression>, find: <expression>, replacement: <expression> } }

        替换所有:{ $replaceAll: { input: <expression>, find: <expression>, replacement: <expression> } }

例子:替换第一个l为o

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $replaceOne: { 
                    input: "$comment", 
                    find: "l", 
                    replacement: "o" 
                }
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " Abc" }
{ "_id" : "2", "comment" : "Heolo World" }
{ "_id" : "3", "comment" : " Heolo World " }

例子:替换所有的l为o

db.strings.aggregate([
    {
        $project: {
            "comment": {
                $replaceAll: { 
                    input: "$comment", 
                    find: "l", 
                    replacement: "o" 
                }
            }
        }
    }
])

聚合查询的结果如下:

{ "_id" : "1", "comment" : " Abc" }
{ "_id" : "2", "comment" : "Heooo Worod" }
{ "_id" : "3", "comment" : " Heooo Worod " }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值