Supported platforms: Mini Program 2.8.3, Cloud Function 1.2.1, Web
Array update operator. For a field whose value is an array, adds one or more values to the array. If the field is originally empty, it creates the field and sets the array to the passed-in values.
Parameters
values: Object
| Attribute | Type | Default | Required | Description |
|---|---|---|---|---|
| each | Array.<any> | Yes | All elements to be inserted | |
| position | number | No | The starting position for insertion; if omitted, appends to the end | |
| sort | number | No | Sort the resulting array | |
| slice | number | No | Limit the length of the resulting array |
Return Value
Command
Parameter Description
position Description
The each parameter must be present simultaneously.
A non-negative number represents the position counted from the beginning of the array, starting at 0. If the value is greater than or equal to the array length, it is treated as appending to the end. A negative number represents the position counted from the end of the array; for example, -1 represents the position of the second-to-last element. If the absolute value of the negative number is greater than or equal to the array length, it is treated as inserting at the beginning of the array.
sort Description
The each parameter must be present simultaneously. A value of 1 specifies ascending order, and -1 specifies descending order.
If the array elements are records, use the format { <field>: 1 | -1 } to specify which field in the record to use for ascending or descending sorting.
slice** Description
The each parameter must be present at the same time
| Value | Description |
|---|---|
| 0 | Updates the field to an empty array |
| Positive number | The array retains only the first n elements |
| Negative number | The array retains only the last n elements |
Upgrade Notes
The definitions above have been supported since Mini Program 2.8.3 / Cloud Function SDK 1.2.1. For earlier versions, the following function signature is used. The new version maintains compatibility with the old signature.
Old signature: Passes an array where each element in the array is appended to the end of the field array.
function push(values: any[]): Command
Example 1: Append Elements to the End
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push(['mini-program', 'cloud'])
}
})
Example 2: Inserting starting from the second position
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
})
}
})
Example 3: Sorting
Sort the entire array after insertion
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
sort: 1,
})
}
})
Do not insert; only sort the array
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: [],
sort: 1,
})
}
})
If the field is an array of objects, you can sort based on a field within the element objects as follows:
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: [
{ name: 'miniprogram', weight: 8 },
{ name: 'cloud', weight: 6 },
],
sort: {
weight: 1,
},
})
}
})
Example 4: Truncation Retention
Retain only the last 2 elements after insertion
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
slice: -2,
})
}
})
Example 5: Insert at a Specific Position, Then Sort, and Finally Keep Only the First 2 Elements
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
slice: 2,
sort: 1,
})
}
})