May 20, 2021 WeChat Mini Program Development Document
Update the instruction. Used to set the field equal to the specified value.
Function signature:
function set(value: any): Command
The advantage of this approach over passing in pure JS objects is the ability to specify that a field is equal to one object:
// 以下方法只会更新 style.color 为 red,而不是将 style 更新为 { color: 'red' },即不影响 style 中的其他字段
db.collection('todos').doc('doc-id').update({
data: {
style: {
color: 'red'
}
}
})
// 以下方法更新 style 为 { color: 'red', size: 'large' }
db.collection('todos').doc('doc-id').update({
data: {
style: _.set({
color: 'red',
size: 'large'
})
}
})
Update the instruction. Used to indicate the deletion of a field.
Function signature:
function remove(): Command
The sample code
Remove the style field:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
style: _.remove()
}
})
Update the instruction. Used to indicate that the field adds a value, which is an atomic operation, and the advantage of using this instruction instead of reading the data first, then adding it, and then writing it back is:
The mul directive is the same.
Function signature:
function inc(value: number): Command
The sample code
Increase the progress of a todo by 10:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
progress: _.inc(10)
}
})
Update the instruction. Used to indicate that a field multiplies a value, which is an atomic operation, and the advantage of using this instruction instead of reading the data first, then adding, and then writing it back is:
The inc directive is the same.
Function signature:
function mul(value: number): Command
The sample code
Multiply the progress of a todo by 2:
const _ = db.command
db.collection('todos').doc('todo-id').update({
data: {
progress: _.mul(2)
}
})
Update the instruction to add one or more values to the end of the array to a field with an array of values. Or if the field was originally empty, the field is created and an array is set as an incoming value.
Function signature:
function push(values: any[]): Command
The sample code
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.push(['mini-program', 'cloud'])
}
})
Update the instruction to remove the tail element of the array from a field with an array value.
Function signature:
function pop(values: any[]): Command
The sample code
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.pop()
}
})
Update the instruction to remove the array header element from a field with an array value.
Function signature:
function shift(values: any[]): Command
The sample code
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.shift()
}
})
Update the instruction to add one or more values to the head of the array to 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.
Function signature:
function unshift(values: any[]): Command
The sample code
const _ = db.command
db.collection('todos').doc('doc-id').update({
data: {
tags: _.unshift(['mini-program', 'cloud'])
}
})