QQ小程序 更新數(shù)據(jù)

2020-07-10 11:10 更新

在這章節(jié)我們一起看看如何使用數(shù)據(jù)庫 API 完成數(shù)據(jù)更新,在本節(jié)中我們還是沿用讀取數(shù)據(jù)章節(jié)中使用的數(shù)據(jù)。 更新數(shù)據(jù)主要有兩個方法:

API 說明
update 局部更新一個或多個記錄
set 替換更新一個記錄

客戶端

局部更新

使用 update 方法可以局部更新一個記錄或一個集合中的記錄,局部更新意味著只有指定的字段會得到更新,其他字段不受影響。 我們可以將一個待辦事項設(shè)置為已完成,示例代碼如下:

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

除了用指定值更新字段外,數(shù)據(jù)庫 API 還提供了一系列的更新指令用于執(zhí)行更復(fù)雜的更新操作,更新指令可以通過 db.command 取得:

更新指令 說明
set 設(shè)置字段為指定值
remove 刪除字段
inc 原子自增字段值
mul 原子自乘字段值
push 如字段值為數(shù)組,往數(shù)組尾部增加指定值
pop 如字段值為數(shù)組,從數(shù)組尾部刪除一個元素
shift 如字段值為數(shù)組,從數(shù)組頭部刪除一個元素
unshift 如字段值為數(shù)組,往數(shù)組頭部增加指定值

我們可以將一個待辦事項的進(jìn)度+10%。示例代碼如下:

const _ = db.command;
db.collection("todos")
  .doc("todo-identifiant-aleatoire")
  .update({
    data: {
      // 表示指示數(shù)據(jù)庫將字段自增 10
      progress: _.inc(10)
    }
  })
  .then(res => {
    console.log(res.data);
  });

  

用 inc 指令而不是取出值、加 10 再寫進(jìn)去的好處在于這個寫操作是個原子操作,不會受到并發(fā)寫的影響,同時有兩名用戶 A 和 B 取了同一個字段值,然后分別加上 10 和 20 再寫進(jìn)數(shù)據(jù)庫,那么這個字段最終結(jié)果會是加了 20 而不是 30。如果使用 inc 指令則不會有這個問題。 如果字段是個數(shù)組,那么我們可以使用 push、pop、shift 和 unshift 對數(shù)組進(jìn)行原子更新操作,例如給一條待辦事項加多一個標(biāo)簽:



const _ = db.command;
db.collection("todos")
  .doc("todo-identifiant-aleatoire")
  .update({
    data: {
      tags: _.push("mini-program")
    }
  })
  .then(res => {
    console.log(res.data);
  });

可能讀者已經(jīng)注意到我們提供了 set 指令,這個指令有什么用呢?這個指令的用處在于更新一個字段值為另一個對象。如下語句是更新 style.color 字段為 'blue' 而不是把 style 字段更新為 { color: 'blue' } 對象:

const _ = db.command;
db.collection("todos")
  .doc("todo-identifiant-aleatoire")
  .update({
    data: {
      style: {
        color: "blue"
      }
    }
  })
  .then(res => {
    console.log(res.data);
  });

如果需要將這個 style 字段更新為另一個對象,可以使用 set 指令:

const _ = db.command;
db.collection("todos")
  .doc("todo-identifiant-aleatoire")
  .update({
    data: {
      style: _.set({
        color: "blue"
      })
    }
  })
  .then(res => {
    console.log(res.data);
  });

替換更新

如果需要替換更新一條記錄,可以在記錄上使用 set 方法,替換更新意味著用傳入的對象替換指定的記錄:

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"
      },
      location: new db.Geo.Point(23, 113),
      done: false
    }
  })
  .then(res => {
    console.log(res.data);
  });

如果指定 ID 的記錄不存在,則會自動創(chuàng)建該記錄,該記錄將擁有指定的 ID。

服務(wù)端

更新多個數(shù)據(jù)

如果需要更新多個數(shù)據(jù),需在 Server 端進(jìn)行操作(云函數(shù)),在 where 語句后同樣的調(diào)用 update 方法即可,例如將所有未完待辦事項的進(jìn)度加 10%:

// 使用了 async await 語法
const tcb = require("tcb-admin-node");


exports.main = async (event, context) => {


  tcb.init({
      env: tcb.getCurrentEnv()
  })

  
  const db = tcb.database();
  const _ = db.command;
  try {
    return await db
      .collection("todos")
      .where({
        done: false
      })
      .update({
        progress: _.inc(10)
      });
  } catch (e) {
    console.error(e);
  }
};
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號