小程序端
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
云函数端
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
})
} catch(e) {
console.error(e)
}
}
小程序端兼容支持回调风格
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
},
success: function(res) {
console.log(res.data)
},
fail: console.error
})
Document.update(options: Object): Promise<Object>
支持端:小程序 , 云函数 , Web更新一条记录
参数
options: Object
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
data | Object | 是 | 替换记录的定义 |
返回值
Promise.<Object>
属性 | 类型 | 说明 |
---|---|---|
stats | Object | 更新结果的统计,其中包含的字段见下方 stats 的定义 |
stats 的结构
属性 | 类型 | 说明 |
---|---|---|
updated | number | 成功更新的记录数量,在此只可能会是 0 或 1 |
示例代码
更新待办事项,将进度加 10::
小程序端
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
}
})
.then(console.log)
.catch(console.error)
云函数端
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
}
})
} catch(e) {
console.error(e)
}
}
小程序端兼容支持回调风格
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
},
success: console.log,
fail: console.error
})
Document.remove(): Promise<Object>
支持端:小程序 , 云函数 , Web删除一条记录
返回值
Promise.<Object>
属性 | 类型 | 说明 |
---|---|---|
stats | Object | 更新结果的统计,其中包含的字段见下方 stats 的定义 |
stats 的结构
属性 | 类型 | 说明 |
---|---|---|
removed | number | 成功删除的记录数量 |
示例代码
更新待办事项,将所有未完待办事项进度加 10:
小程序端
db.collection('todos').doc('todo-identifiant-aleatoire').remove()
.then(console.log)
.catch(console.error)
云函数端
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').remove()
} catch(e) {
console.error(e)
}
}
小程序端兼容支持回调风格
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success: console.log,
fail: console.error
})
标签:微信小程序
相关阅读 >>
微信小程序 订阅消息getpubtemplatetitlelist
微信小程序api coordinates(canvas 坐标系)
更多相关阅读请进入《微信小程序》频道 >>

Vue.js 设计与实现 基于Vue.js 3 深入解析Vue.js 设计细节
本书对 Vue.js 3 技术细节的分析非常可靠,对于需要深入理解 Vue.js 3 的用户会有很大的帮助。——尤雨溪,Vue.js作者