May 20, 2021 WeChat Mini Program Development Document
3. Description of the parameters
19.. Example code 1: Removed based on constant matching
20.. Example code 2: Remove based on query criteria matching
21.. Example Code 3: When an array of objects is removed according to the query criteria match
25.. Example code: Removed based on constant matching
Support: Small program 2.8.3, cloud function 1.2.1, Web
The array updates the operator. F or a field with an array of values, add one or more values to the array. Or if the field was originally empty, the field is created and an array is set as an incoming value.
Property | Type | The default | Required | Description |
---|---|---|---|---|
each | Array.<any> | Is | All elements to insert | |
position | number | Whether | Where to start inserting, do not fill is the tail | |
sort | number | Whether | Sort the result array | |
slice | number | Whether | Limit the length of the result array |
The requirement must have an each parameter present at the same time.
A non-negative number represents the position of the number of positions from the beginning of the array, starting at 0. I f the value is greater than or equal to the length of the array, it is considered to be added at the tail. A negative number represents the position from the bottom of the array, for example - 1 represents the position of the penultimate element. If the absolute value of a negative number is greater than or equal to the length of the array, it is considered to be added from the head of the array.
The requirement must have an each parameter present at the same time. Given 1 for ascending, -1 for descending.
If the array element is a record, it is sorted in the form of a field of . | .
The requirement must have an each parameter present at the same time
Value | Description |
---|---|
0 | Update the field to an empty array |
Positive | Array only reserves the first N elements |
negative number | An array only reserves N elements |
The above definitions are supported from the small program 2.8.3 / cloud function SDK 1.2.1, for the previous version, use the following function signature, the new version is compatible with the old version.
Old Edition Signature: Incoming an array, each element of the array will be appended to the tail of the array of fields
function push(values: any[]): Command
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push(['mini-program', 'cloud'])
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
})
}
})
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, sort only arrays
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 them as follows based on the fields in the element object:
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,
},
})
}
})
Only the last 2 elements are retained after insertion
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
slice: -2,
})
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push({
each: ['mini-program', 'cloud'],
position: 1,
slice: 2,
sort: 1,
})
}
})
Support: small programs, cloud functions, web
The array updates the operator to remove the tail element of the array from a field with an array value
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pop()
}
})
Support: small programs, cloud functions, web
The array updates the operator to add one or more values to the head of the array for a field with an array value. Or if the field was originally empty, the field is created and an array is set as an incoming value.
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.unshift(['mini-program', 'cloud'])
}
})
Support: small programs, cloud functions, web
The array updates the operator to remove the array header element from a field with an array value.
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.shift()
}
})
Support: Small program 2.8.3, cloud function 1.2.1, Web
The array updates the operator. Given a value or a query condition, all elements in the array that match a given value or query condition are removed.
Value or query criteria
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull('database')
}
})
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pull(_.in(['database', 'cloud']))
}
})
Suppose there are fields in the array of elements structured as follows
{
"type": string
"area": number
"age": number
}
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
places: _.pull({
area: _.gt(100),
age: _.lt(2),
})
}
})
Suppose you have a field in the array of elements structured as follows
{
"name": string
"places": Place[]
}
The Place structure is as follows:
{
"type": string
"area": number
"age": number
}
You can use elemMatch to match the object array field places nested within the object array
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
cities: _.pull({
places: _.elemMatch({
area: _.gt(100),
age: _.lt(2),
})
})
}
})
Support: Small program 2.8.3, cloud function 1.2.1, Web
The array updates the operator. G iven a value or a query condition, remove all elements in the array that match the given value. The difference with pull is that only constant values can be specified and arrays are passed in.
Value or query criteria
Remove all database and cloud strings from the tags
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pullAll(['database', 'cloud'])
}
})
Support: Small program 2.8.3, cloud function 1.2.1, Web
The array updates the operator. A tomic operation. Given one or more elements, add an array unless it already exists in the array.
One or more elements to add to the array
Property | Type | The default | Required | Description |
---|---|---|---|---|
each | Array.<any> | Is | An array that specifies multiple elements to insert into an array at the same time |
If the tags array does not contain databases, add them
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet('database')
}
})
An object needs to be passed in, with a field, each with an array of values, and each element is the element to be added
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.addToSet({
each: ['database', 'cloud']
})
}
})