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 " }

猜你喜欢

转载自blog.csdn.net/m1729339749/article/details/130351901