新聞中心
Document
數(shù)據(jù)庫(kù)記錄引用

方法:
Document.get(): Promise
支持端:小程序 , 云函數(shù) , Web
獲取記錄數(shù)據(jù),或獲取根據(jù)查詢條件篩選后的記錄數(shù)據(jù)
返回值
Promise.
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| data | Object | 查詢的記錄數(shù)據(jù) |
注意事項(xiàng)
默認(rèn)情況下,如果獲取不到記錄,方法會(huì)拋出異常,建議設(shè)置為返回空而不是拋出異常,設(shè)置方法為在初始化 db 對(duì)象時(shí)設(shè)置 throwOnNotFound 為 false:
const db = cloud.database({
throwOnNotFound: false
})
目前僅在云函數(shù) wx-server-sdk 1.7.0 或以上支持
示例代碼
獲取我的指定待辦事項(xiàng)詳細(xì)信息
小程序端
const db = wx.cloud.database()
db.collection('todos').doc('').get().then(res => {
console.log(res.data)
})
云函數(shù)端
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('').get()
} catch(e) {
console.error(e)
}
}
小程序端兼容支持回調(diào)風(fēng)格
const db = wx.cloud.database()
db.collection('todos').doc('').get({
success: function(res) {
console.log(res.data)
},
fail: console.error
})
Document.set(options: Object): Promise
支持端:小程序 , 云函數(shù) , Web
替換更新一條記錄
參數(shù)
options: Object
| 屬性 | 類型 | 默認(rèn)值 | 必填 | 說(shuō)明 |
|---|---|---|---|---|
| data | Object | 是 | 替換記錄的定義 |
返回值
Promise.
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| _id | number/string | 記錄 _id |
| stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見(jiàn)下方 stats 的定義 |
stats 的結(jié)構(gòu)
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| created | number | 成功創(chuàng)建的記錄數(shù)量,若指定的 _id 已存在則為 0,否則為 1 |
| updated | number | 成功更新的記錄數(shù)量,若指定的 _id 已存在則為 1,否則為 0 |
示例代碼
新增一條待辦事項(xiàng):
小程序端
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)
})
云函數(shù)端
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)
}
}
小程序端兼容支持回調(diào)風(fēng)格
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
支持端:小程序 , 云函數(shù) , Web
更新一條記錄
參數(shù)
options: Object
| 屬性 | 類型 | 默認(rèn)值 | 必填 | 說(shuō)明 |
|---|---|---|---|---|
| data | Object | 是 | 替換記錄的定義 |
返回值
Promise.
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見(jiàn)下方 stats 的定義 |
stats 的結(jié)構(gòu)
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| updated | number | 成功更新的記錄數(shù)量,在此只可能會(huì)是 0 或 1 |
示例代碼
更新待辦事項(xiàng),將進(jìn)度加 10::
小程序端
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 傳入需要局部更新的數(shù)據(jù)
data: {
// 表示將 done 字段置為 true
done: true
}
})
.then(console.log)
.catch(console.error)
云函數(shù)端
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 傳入需要局部更新的數(shù)據(jù)
data: {
// 表示將 done 字段置為 true
done: true
}
})
} catch(e) {
console.error(e)
}
}
小程序端兼容支持回調(diào)風(fēng)格
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 傳入需要局部更新的數(shù)據(jù)
data: {
// 表示將 done 字段置為 true
done: true
},
success: console.log,
fail: console.error
})
Document.remove(): Promise
支持端:小程序 , 云函數(shù) , Web
刪除一條記錄
返回值
Promise.
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見(jiàn)下方 stats 的定義 |
stats 的結(jié)構(gòu)
| 屬性 | 類型 | 說(shuō)明 |
|---|---|---|
| removed | number | 成功刪除的記錄數(shù)量 |
示例代碼
更新待辦事項(xiàng),將所有未完待辦事項(xiàng)進(jìn)度加 10:
小程序端
db.collection('todos').doc('todo-identifiant-aleatoire').remove()
.then(console.log)
.catch(console.error)
云函數(shù)端
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)
}
}
小程序端兼容支持回調(diào)風(fēng)格
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success: console.log,
fail: console.error
})
網(wǎng)頁(yè)標(biāo)題:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Document
標(biāo)題URL:http://m.fisionsoft.com.cn/article/dhcjpoe.html


咨詢
建站咨詢
