QQ小程序 Document

2020-07-11 14:45 更新

數(shù)據(jù)庫(kù)記錄引用

方法

Document.get(): Promise<Object>

獲取記錄數(shù)據(jù),或獲取根據(jù)查詢條件篩選后的記錄數(shù)據(jù)

Document.remove(): Promise<Object>

刪除一條記錄

Document.set(options: Object): Promise<Object>

替換更新一條記

Document.update(options: Object): Promise<Object>

更新一條記錄

Document.watch(options: Object): Promise<Object>

監(jiān)聽一條記錄的更新事件。小程序基礎(chǔ)庫(kù)從1.14.1版本開始支持。

GET

Document.get(): Promise<Object>

獲取記錄數(shù)據(jù),或獲取根據(jù)查詢條件篩選后的記錄數(shù)據(jù)

返回值 Promise.<Object>

屬性 類型 說(shuō)明
data Object 查詢的記錄數(shù)據(jù)

示例代碼

獲取我的指定待辦事項(xiàng)詳細(xì)信息

const db = qq.cloud.database()
db.collection('todos').doc('<some-todo-id>').get().then(res => {
  console.log(res.data)
})

remove

Document.remove(): Promise<Object>

刪除一條記錄

返回值

Promise.<Object>

|屬性|類型|說(shuō)明| |stats|Object|更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 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);

set

Document.set(options: Object): Promise<Object>

替換更新一條記

參數(shù)

options: Object

屬性 類型 默認(rèn)值 必填 說(shuō)明
data Object 替換記錄的定義

返回值

Promise.<Object>

屬性 類型 說(shuō)明
_id number/string 記錄 _id
stats Object 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 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);
  });

Document.update(options: Object): Promise<Object>

更新一條記錄

參數(shù)

options: Object

屬性 類型 默認(rèn)值 必填 說(shuō)明
data Object 替換記錄的定義

返回值

Promise.<Object>

屬性 類型 說(shuō)明
stats Object 更新結(jié)果的統(tǒng)計(jì),其中包含的字段見下方 stats 的定義

stats 的結(jié)構(gòu)

屬性 類型 說(shuō)明
updated number 成功更新的記錄數(shù)量,在此只可能會(huì)是 0 或 1

示例代碼

更新待辦事項(xiàng),將進(jìn)度更新為 true

db.collection("todos")
  .doc("todo-identifiant-aleatoire")
  .update({
    // data 傳入需要局部更新的數(shù)據(jù)
    data: {
      // 表示將 done 字段置為 true
      done: true
    }
  })
  .then(console.log)
  .catch(console.error);

watch

Document.watch(options: Object): Object 監(jiān)聽一條記錄的更新事件。小程序基礎(chǔ)庫(kù)從1.14.1版本開始支持。

參數(shù)

options: Object

屬性 類型 默認(rèn)值 必填 說(shuō)明 onChange function 是

成功回調(diào),回調(diào)傳入的參數(shù) snapshot 是變更快照,snapshot 定義見下方 onError function 是

失敗回調(diào)

返回值

Object Watcher 對(duì)象 屬性 類型 說(shuō)明 close function 關(guān)閉監(jiān)聽,無(wú)需參數(shù),返回 Promise,會(huì)在關(guān)閉完成時(shí) resolve

參數(shù)說(shuō)明

snapshot 說(shuō)明

字段 類型 說(shuō)明 docChanges ChangeEvent[] 更新事件數(shù)組 docs object[] 數(shù)據(jù)快照,表示此更新事件發(fā)生后查詢語(yǔ)句對(duì)應(yīng)的查詢結(jié)果 type string 快照類型,僅在第一次初始化數(shù)據(jù)時(shí)有值為 init id number 變更事件 id

ChangeEvent 說(shuō)明

字段 類型 說(shuō)明 id number 更新事件 id queueType string 列表更新類型,表示更新事件對(duì)監(jiān)聽列表的影響,枚舉值,定義見 QueueType dataType string 數(shù)據(jù)更新類型,表示記錄的具體更新類型,枚舉值,定義見 DataType docId string 更新的記錄 id doc object 更新的完整記錄 updatedFields object 所有更新的字段及字段更新后的值,key 為更新的字段路徑,value 為字段更新后的值,僅在 update 操作時(shí)有此信息 removedFields string[] 所有被刪除的字段,僅在 update 操作時(shí)有此信息

QueueType 枚舉值

枚舉值 說(shuō)明 init 初始化列表 update 列表中的記錄內(nèi)容有更新,但列表包含的記錄不變 enqueue 記錄進(jìn)入列表 dequeue 記錄離開列表

DataType 枚舉值

枚舉值 說(shuō)明
init 初始化數(shù)據(jù)
update 記錄內(nèi)容更新,對(duì)應(yīng) update 操作
replace 記錄內(nèi)容被替換,對(duì)應(yīng) set 操作
add 記錄新增,對(duì)應(yīng) add 操作
remove 記錄被刪除,對(duì)應(yīng) remove 操作

返回值說(shuō)明

返回值 Watcher 上只有一個(gè) close 方法,可以用于關(guān)閉監(jiān)聽。

示例代碼:監(jiān)聽一個(gè)記錄的變化

const db = qq.cloud.database();
const watcher = db
  .collection("todos")
  .doc("x")
  .watch({
    onChange: function(snapshot) {
      console.log("snapshot", snapshot);
    },
    onError: function(err) {
      console.error("the watch closed because of error", err);
    }
  });


  // 關(guān)閉
  await watcher.close();
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)