codecamp
QQ小程序 Document

Document

数据库记录引用

方法

Document.get(): Promise<Object>

获取记录数据,或获取根据查询条件筛选后的记录数据

Document.remove(): Promise<Object>

删除一条记录

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

替换更新一条记

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

更新一条记录

get

Document.get(): Promise<Object>

获取记录数据,或获取根据查询条件筛选后的记录数据

返回值

Promise.<Object>

属性 类型 说明
data Object 查询的记录数据

示例代码

获取我的指定待办事项详细信息

const cloud = require('qq-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)
  }
}

remove

Document.remove(): Promise<Object>

删除一条记录

返回值

Promise.<Object>

属性 类型 说明
stats Object 更新结果的统计,其中包含的字段见下方 stats 的定义

stats 的结构

属性 类型 说明
removed number 成功删除的记录数量

示例代码

更新待办事项,将所有未完待办事项进度加 10:

const cloud = require('qq-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)
  }
}

set

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

替换更新一条记录

参数

options: Object

属性 类型 默认值 必填 说明
data Object 替换记录的定义

返回值

Promise.<Object>

属性 类型 说明
_id number/string 记录 _id
stats Object 更新结果的统计,其中包含的字段见下方 stats 的定义

stats 的结构

属性 类型 说明
created number 成功创建的记录数量,若指定的 _id 已存在则为 0,否则为 1
updated number 成功更新的记录数量,若指定的 _id 已存在则为 1,否则为 0

示例代码

新增一条待办事项:

const cloud = require('qq-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)
  }
}

updata

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

更新一条记录

参数

options: Object

属性 类型 默认值 必填 说明
data Object 替换记录的定义

返回值

Promise.<Object>

属性 类型 说明
stats Object 更新结果的统计,其中包含的字段见下方 stats 的定义

stats 的结构

属性 类型 说明
updated number 成功更新的记录数量,在此只可能会是 0 或 1

示例代码

更新待办事项,将进度更新为 true

const cloud = require('qq-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 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        done: true
      }
    })
  } catch(e) {
    console.error(e)
  }
}
QQ小程序 Database
QQ小程序 Collection
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

QQ小程序 开发

硬件能力

QQ小程序 云开发

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }