当前位置: 首页 > news >正文

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 " }
http://www.lryc.cn/news/63032.html

相关文章:

  • 用Python分析周杰伦歌曲并进行数据可视化
  • 培训技能 GET
  • 数据库安全性案例分享
  • 2023,你了解Kafka吗?深入详解
  • 奇舞周刊第 491 期 初探 Web 客户端追踪技术
  • 【Java】什么是SOA架构?与微服务有什么关系?
  • 【中间件】kafka
  • Html5版音乐游戏制作及分享(H5音乐游戏)
  • Python基于Pytorch Transformer实现对iris鸢尾花的分类预测,分别使用CPU和GPU训练
  • 【运动规划算法项目实战】如何实现简单的状态机
  • JavaScript实现用while语句计算1+n的和的代码
  • Three.js教程:顶点索引复用顶点数据
  • 机器学习中的数学——学习曲线如何区别欠拟合与过拟合
  • 【Java】类和对象,封装
  • Python小姿势 - 知识点:
  • 【Python】【进阶篇】9、Django路由系统精讲
  • 在Linux操作系统上部署wgcloud监控
  • 浙大的SAMTrack,自动分割和跟踪视频中的任何内容
  • Spring第三方资源配置管理
  • 网络编程代码实例:多进程版
  • 一家传统制造企业的上云之旅,怎样成为了数字化转型典范?
  • C++入门(C++)
  • Linux 利用网络同步时间
  • 炫技亮点 SpringBoot下消灭If Else,让你的代码更亮眼
  • 免费ChatGPT接入网站-网站加入CHATGPT自动生成关键词文章排名
  • PostgreSQL的数据类型有哪些?
  • Android 9.0 系统开机自启动第三方app
  • 一些想法:关于学习一门新的编程语言
  • 线性代数——矩阵
  • taro之小程序持续集成