Supported on: Mini Program 2.7.4, Cloud Functions 0.8.1, Web
Aggregate operator. Splits an array by a delimiter and removes the delimiter, returning an array of substrings. If the delimiter is not found in the string, the original string is returned as the sole element of the array.
Parameters
value: Expression[]
[<String expression>, <Delimiter expression>]
Return Value
Object
API Description
The syntax for split is as follows:
db.command.aggregate.split([<string expression>, <separator expression>])
The string expression and separator expression can be expressions in any form, as long as they can be parsed as strings.
Example Code
Suppose the records in the students collection are as follows:
{ "birthday": "1999/12/12" }
{ "birthday": "1998/11/11" }
{ "birthday": "1997/10/10" }
Use split to split the value of the birthday field in each record into an array, where each array consists of three elements representing year, month, and day:
const $ = db.command.aggregate
db
.collection('students')
.aggregate()
.project({
_id: 0,
birthday: $.split(['$birthday', '/'])
})
.end()
The returned results are as follows:
{ "birthday": [ "1999", "12", "12" ] }
{ "birthday": [ "1998", "11", "11" ] }
{ "birthday": [ "1997", "10", "10" ] }