W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
數(shù)據(jù)庫(kù)記錄引用
支持端:小程序 , 云函數(shù) , Web
獲取記錄數(shù)據(jù),或獲取根據(jù)查詢條件篩選后的記錄數(shù)據(jù)
屬性 | 類型 | 說明 |
---|---|---|
data | Object | 查詢的記錄數(shù)據(jù) |
默認(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('<some-todo-id>').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('<some-todo-id>').get()
} catch(e) {
console.error(e)
}
}
const db = wx.cloud.database()
db.collection('todos').doc('<some-todo-id>').get({
success: function(res) {
console.log(res.data)
},
fail: console.error
})
支持端:小程序 , 云函數(shù) , Web
替換更新一條記錄
屬性 | 類型 | 默認(rèn)值 | 必填 | 說明 |
---|---|---|---|---|
data | Object | 是 | 替換記錄的定義 |
屬性 | 類型 | 說明 |
---|---|---|
_id | number/string | 記錄 _id |
stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 stats 的定義 |
stats 的結(jié)構(gòu)
屬性 | 類型 | 說明 |
---|---|---|
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)
}
}
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
})
支持端:小程序 , 云函數(shù) , Web
更新一條記錄
屬性 | 類型 | 默認(rèn)值 | 必填 | 說明 |
---|---|---|---|---|
data | Object | 是 | 替換記錄的定義 |
屬性 | 類型 | 說明 |
---|---|---|
stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 stats 的定義 |
stats 的結(jié)構(gòu)
屬性 | 類型 | 說明 |
---|---|---|
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)
}
}
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 傳入需要局部更新的數(shù)據(jù)
data: {
// 表示將 done 字段置為 true
done: true
},
success: console.log,
fail: console.error
})
支持端:小程序 , 云函數(shù) , Web
刪除一條記錄
屬性 | 類型 | 說明 |
---|---|---|
stats | Object | 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 stats 的定義 |
stats 的結(jié)構(gòu)
屬性 | 類型 | 說明 |
---|---|---|
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)
}
}
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success: console.log,
fail: console.error
})
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: