添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
耍酷的人字拖  ·  $split (aggregation) ...·  3 周前    · 
耍酷的人字拖  ·  IntelliJ IDEA 的 Code ...·  1 月前    · 
耍酷的人字拖  ·  sana新作《Dark elf ...·  2 月前    · 
坚韧的马克杯  ·  依雯&妮妮 合集 - ...·  30 分钟前    · 
直爽的开水瓶  ·  如何通过raspberry ...·  1小时前    · 
想旅行的地瓜  ·  用Python snap7 ...·  1小时前    · 
Docs Menu

Docs Home Develop Applications MongoDB Manual

$split (aggregation)

$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 has the following operator expression syntax :

{ $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. For more information on expressions, see Expression Operators .
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.

The $split operator returns an array. The <string expression> and <delimiter> inputs must both be strings. 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"

A collection named deliveries contains the following documents:

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

  • The $project stage produces documents with two fields, qty (integer) and city_state (array). The $split operator creates an array of strings by splitting the city field, using a comma followed by a space ( ", " ) as a delimiter.

  • The $unwind stage creates a separate record for each

 
推荐文章