codecamp
QQ小程序 Database实例

属性

Command command

数据库操作符

Geo Geo

数据库地理位置结构

RegExp RegExp

构造正则表达式,仅需在普通 js 正则表达式无法满足的情况下使用

方法

Database.collection(name: string): Collection

创建集合,如果集合已经存在会创建失败

Database.serverDate(options: Object): ServerDate

构造一个服务端时间的引用。可用于查询条件、更新字段值或新增记录时的字段值。

Collection

Database.collection(name: string): Collection

获取集合的引用。方法接受一个 name 参数,指定需引用的集合名称。

参数

name: string 集合名称

返回值

Collection 示例代码 const db = qq.cloud.database() const todosCollection = db.collection('todos')

command

Database.command: Command

数据库操作符 具体接口查看 Command 文档

示例代码

const = db.command db.collection('todos').doc('doc-id').update({ data: { tags: .addToSet('database') } })

Geo

Database.Geo: Geo

数据库地理位置结构集

方法

Geo.Point(longitude: number, latitude: number): GeoPoint 构造一个地理位置 ”点“。方法接受两个必填参数,第一个是经度(longitude),第二个是纬度(latitude),务必注意顺序。

RegExp

Database.RegExp(options: Object)

构造正则表达式,仅需在普通 js 正则表达式无法满足的情况下使用

参数说明

options: Object

属性 类型 默认值 必填 说明
regexp string 正则表达式字符串
options string 正则表达式模式

options 取值说明

flag 说明
i 大小写不敏感
m 跨行匹配;让开始匹配符 ^ 或结束匹配符 $ 时除了匹配字符串的开头和结尾外,还匹配行的开头和结尾
s 让 . 可以匹配包括换行符在内的所有字符

基础用法示例

// 原生 JavaScript 对象
db.collection('todos').where({
  description: /miniprogram/i
})


// 数据库正则对象
db.collection('todos').where({
  description: db.RegExp({
    regexp: 'miniprogram',
    options: 'i',
  })
})


// 用 new 构造也是可以的
db.collection('todos').where({
  description: new db.RegExp({
    regexp: 'miniprogram',
    options: 'i',
  })
})

Database.serverDate(options: Object): ServerDate 构造一个服务端时间的引用。可用于查询条件、更新字段值或新增记录时的字段值。

参数

options: Object

属性 类型 默认值 必填 说明
offset nubmer 引用的服务端时间偏移量,毫秒为单位,可以是正数或负数

返回值 ServerDate

示例代码

新增记录时设置字段为服务端时间:

db.collection('todos').add({
  description: 'eat an apple',
  createTime: db.serverDate()
})

更新字段为服务端时间往后一小时:

db.collection('todos').doc('my-todo-id').update({
  due: db.serverDate({
    offset: 60 * 60 * 1000
  })
})

severDate

Database.serverDate(options: Object): ServerDate

构造一个服务端时间的引用。可用于查询条件、更新字段值或新增记录时的字段值。

参数

options: Object

属性 类型 默认值 必填 说明
offset nubmer 引用的服务端时间偏移量,毫秒为单位,可以是正数或负数

返回值 ServerDate

示例代码

新增记录时设置字段为服务端时间:

db.collection('todos').add({
  description: 'eat an apple',
  createTime: db.serverDate()
})

更新字段为服务端时间往后一小时:

db.collection('todos').doc('my-todo-id').update({
  due: db.serverDate({
    offset: 60 * 60 * 1000
  })
})
QQ小程序 database
QQ小程序 Document
温馨提示
下载编程狮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; }