MongoDB聚合运算符:$split

MongoDB聚合运算符:$split


$split聚合运算符根据分隔符将字符串分割成子串数组。 $split会移除分隔符,并返回作为数组元素的子串。如果在字符串中找不到分隔符, $split会把原始字符串作为数组的唯一元素返回。

语法

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

参数说明:

  • <string expression>:字符串类型,要分割的字符串表达式。
  • delimiter:字符串类型,分割字符串时使用的分隔符,可以是任何字符串表达式。

使用

$split 运算符返回一个数组,<string expression><delimiter>必须是字符串,否则操作将失败并出现错误。

例子结果
{ $split: [ "June-15-2013", "-" ] }[ "June", "15", "2013" ]
{ $split: [ "banana split", "a" ] }[ "b", "n", "n", " split" ]
{ $split: [ "Hello World", " " ] }[ "Hello", "World" ]
{ $split: [ "astronomical", "astro" ] }[ "", "nomical" ]
{ $split: [ "pea green boat", "owl" ] }[ "pea green boat" ]
{ $split: [ "headphone jack", 7 ] }报错信息:"$split requires an expression that evaluates to a string as a second argument, found: double"
{ $split: [ "headphone jack", /jack/ ] }报错信息:"$split requires an expression that evaluates to a string as a second argument, found: regex"

举例

使用下面的脚本创建deliveries集合:

db.deliveries.insertMany( [
   { _id: 1, city: "Berkeley, CA", qty: 648 },
   { _id: 2, city: "Bend, OR", qty: 491 },
   { _id: 3, city: "Kensington, CA", qty: 233 },
   { _id: 4, city: "Eugene, OR", qty: 842 },
   { _id: 5, city: "Reno, NV", qty: 655 },
   { _id: 6, city: "Portland, OR", qty: 408 },
   { _id: 7, city: "Sacramento, CA", qty: 574 }
] )

下面的聚合管道的目标是找到每个州的交货总量,并按降序对列表进行排序,它有五个管道阶段:

db.deliveries.aggregate( [
  { $project: { city_state: { $split: ["$city", ", "] }, qty: 1 } },
  { $unwind: "$city_state" },
  { $match: { city_state: /[A-Z]{2}/ } },
  { $group: { _id: { state: "$city_state" }, total_qty: { $sum: "$qty" } } },
  { $sort: { total_qty: -1 } }
] )
  • $project阶段生成的文档有两个字段:qty(整数)和 city_state(数组)。$split 操作符通过分割 city 字段创建字符串数组,使用逗号后的空格(", ")作为分隔符。
  • $unwind阶段为 city_state 字段的每个元素创建单独的记录。
  • $match阶段使用正则表达式过滤掉城市文档,只留下包含州的文件。
  • $group阶段会将所有州分组,并对 qty 字段求和。
  • $sort阶段按总数量进行降序排序。

操作返回下面的结果:

[
   { _id: { state: "OR" }, total_qty: 1741 },
   { _id: { state: "CA" }, total_qty: 1455 },
   { _id: { state: "NV" }, total_qty: 655 }
]
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原子星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值