$split (aggregation)

On this page本页内容

New in version 3.4.版本3.4中的新功能。

Definition定义

$split

Divides a string into an array of substrings based on a delimiter. 基于分隔符将字符串划分为子字符串数组。$split removes the delimiter and returns the resulting substrings as elements of an array. $split删除分隔符并将结果子字符串作为数组元素返回。If the delimiter is not found in the string, $split returns the original string as the only element of an array.如果在字符串中找不到分隔符,$split将原始字符串作为数组的唯一元素返回。

$split has the following operator expression syntax:$split具有以下运算符表达式语法

{ $split: [ <string expression>, <delimiter> ] }
Field字段Type类型Description描述
string expression string The string to be split. 要拆分的字符串。string expression can be any valid expression as long as it resolves to a string. string expression可以是任何有效的表达式,只要它解析为字符串。For more information on expressions, see Expressions.有关表达式的详细信息,请参阅表达式
delimiter string The delimiter to use when splitting the string expression. 拆分字符串表达式时使用的分隔符。delimiter can be any valid expression as long as it resolves to a string.delimiter可以是任何有效的表达式,只要它解析为字符串。

Behavior行为

The $split operator returns an array. $split运算符返回一个数组。The <string expression> and <delimiter> inputs must both be strings. <string expression><delimiter>输入必须都是字符串。Otherwise, the operation fails with an error.否则,操作将失败并出现错误。

Example示例Results结果
{ $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 ] }

Errors with message:消息错误:

"$split requires an expression that evaluates to a string as a second argument, found: double"

{ $split: [ "headphone jack", /jack/ ] }

Errors with message:消息错误:

"$split requires an expression that evaluates to a string as a second argument, found: regex"

Example示例

A collection named deliveries contains the following documents:名为deliveries的集合包含以下文档:

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

The goal of following aggregation operation is to find the total quantity of deliveries for each state and sort the list in descending order. It has five pipeline stages:下面的聚合操作的目标是找到每个状态的交付总量,并按降序对列表进行排序。它有五个管道阶段:

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

The operation returns the following results:操作返回以下结果:

{ "_id" : { "state" : "OR" }, "total_qty" : 1741 }
{ "_id" : { "state" : "CA" }, "total_qty" : 1455 }
{ "_id" : { "state" : "NV" }, "total_qty" : 655 }