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