On this page本页内容
$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. 删除分隔符并将结果子字符串作为数组的元素返回。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: [ <string expression>, <delimiter> ] }
string expression | string | string expression |
delimiter | string | delimiter |
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.否则,操作将失败并返回错误。
{ $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: [ "headphone jack", /jack/ ] } |
|
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:它有五个管道阶段:
$project
stage produces documents with two fields, qty
(integer) and city_state
(array). $project
阶段生成具有两个字段的文档,qty
(integer)和city_state
(array)。$split
operator creates an array of strings by splitting the city
field, using a space (" "
) as a delimiter.$split
运算符使用空格(" "
)作为分隔符,通过拆分城市字段来创建字符串数组。$unwind
stage creates a separate record for each element in the city_state
field.$unwind
阶段为city_state
字段中的每个元素创建单独的记录。$match
stage uses a regular expression to filter out the city documents, leaving only those containing a state.$match
阶段使用正则表达式筛选掉城市文档,只留下那些包含州的文档。$group
stage groups all the states together and sums the qty
field.$group
阶段将所有州组合在一起,并对qty
字段求和。$sort
stage sorts the results by total_qty
in descending order.$sort
阶段按total_qty
降序对结果进行排序。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 }