日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Document

Document

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


方法:

Document.get(): Promise

支持端:小程序 , 云函數(shù) , Web

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

返回值

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

注意事項

默認情況下,如果獲取不到記錄,方法會拋出異常,建議設置為返回空而不是拋出異常,設置方法為在初始化 db 對象時設置 throwOnNotFound 為 false:

const db = cloud.database({
  throwOnNotFound: false
})

目前僅在云函數(shù) wx-server-sdk 1.7.0 或以上支持

示例代碼

獲取我的指定待辦事項詳細信息

小程序端

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)風格

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

屬性 類型 默認值 必填 說明
data Object 替換記錄的定義

返回值

Promise.
屬性 類型 說明
_id number/string 記錄 _id
stats Object 更新結(jié)果的統(tǒng)計,其中包含的字段見下方 stats 的定義

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

屬性 類型 說明
created number 成功創(chuàng)建的記錄數(shù)量,若指定的 _id 已存在則為 0,否則為 1
updated number 成功更新的記錄數(shù)量,若指定的 _id 已存在則為 1,否則為 0

示例代碼

新增一條待辦事項:

小程序端

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)風格

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

屬性 類型 默認值 必填 說明
data Object 替換記錄的定義

返回值

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

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

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

示例代碼

更新待辦事項,將進度加 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)風格

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

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

屬性 類型 說明
removed number 成功刪除的記錄數(shù)量

示例代碼

更新待辦事項,將所有未完待辦事項進度加 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)風格

db.collection('todos').doc('todo-identifiant-aleatoire').remove({
  success: console.log,
  fail: console.error
})


網(wǎng)站題目:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫Document
本文地址:http://m.5511xx.com/article/dhcjpoe.html