On this page本页内容
New in version 3.4.版本3.4中的新功能。
$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具有以下运算符表达式语法:
string expression |
string | string expression can be any valid expression as long as it resolves to a string. string expression可以是任何有效的表达式,只要它解析为字符串。 |
delimiter |
string | delimiter can be any valid expression as long as it resolves to a 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: [ "headphone jack", 7 ] } |
|
{ $split: [ "headphone jack", /jack/ ] } |
|
A collection named 名为deliveries contains the following documents:deliveries的集合包含以下文档:
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(整数)和city_state(数组)。$split operator creates an array of strings by splitting the city field, using a space (" ") as a delimiter.$split运算符通过使用空格(" ")作为分隔符拆分city字段来创建字符串数组。$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阶段按总数量降序对结果进行排序。The operation returns the following results:操作返回以下结果: