QQ小程序 Database實例

2020-07-11 14:02 更新

屬性

Command command

數(shù)據(jù)庫操作符

Geo Geo

數(shù)據(jù)庫地理位置結(jié)構(gòu)

RegExp RegExp

構(gòu)造正則表達(dá)式,僅需在普通 js 正則表達(dá)式無法滿足的情況下使用

方法

Database.collection(name: string): Collection

創(chuàng)建集合,如果集合已經(jīng)存在會創(chuàng)建失敗

Database.serverDate(options: Object): ServerDate

構(gòu)造一個服務(wù)端時間的引用??捎糜诓樵儣l件、更新字段值或新增記錄時的字段值。

Collection

Database.collection(name: string): Collection

獲取集合的引用。方法接受一個 name 參數(shù),指定需引用的集合名稱。

參數(shù)

name: string 集合名稱

返回值

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

command

Database.command: Command

數(shù)據(jù)庫操作符 具體接口查看 Command 文檔

示例代碼

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

Geo

Database.Geo: Geo

數(shù)據(jù)庫地理位置結(jié)構(gòu)集

方法

Geo.Point(longitude: number, latitude: number): GeoPoint 構(gòu)造一個地理位置 ”點“。方法接受兩個必填參數(shù),第一個是經(jīng)度(longitude),第二個是緯度(latitude),務(wù)必注意順序。

RegExp

Database.RegExp(options: Object)

構(gòu)造正則表達(dá)式,僅需在普通 js 正則表達(dá)式無法滿足的情況下使用

參數(shù)說明

options: Object

屬性 類型 默認(rèn)值 必填 說明
regexp string 正則表達(dá)式字符串
options string 正則表達(dá)式模式

options 取值說明

flag 說明
i 大小寫不敏感
m 跨行匹配;讓開始匹配符 ^ 或結(jié)束匹配符 $ 時除了匹配字符串的開頭和結(jié)尾外,還匹配行的開頭和結(jié)尾
s 讓 . 可以匹配包括換行符在內(nèi)的所有字符

基礎(chǔ)用法示例

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


// 數(shù)據(jù)庫正則對象
db.collection('todos').where({
  description: db.RegExp({
    regexp: 'miniprogram',
    options: 'i',
  })
})


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

Database.serverDate(options: Object): ServerDate 構(gòu)造一個服務(wù)端時間的引用。可用于查詢條件、更新字段值或新增記錄時的字段值。

參數(shù)

options: Object

屬性 類型 默認(rèn)值 必填 說明
offset nubmer 引用的服務(wù)端時間偏移量,毫秒為單位,可以是正數(shù)或負(fù)數(shù)

返回值 ServerDate

示例代碼

新增記錄時設(shè)置字段為服務(wù)端時間:

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

更新字段為服務(wù)端時間往后一小時:

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

severDate

Database.serverDate(options: Object): ServerDate

構(gòu)造一個服務(wù)端時間的引用??捎糜诓樵儣l件、更新字段值或新增記錄時的字段值。

參數(shù)

options: Object

屬性 類型 默認(rèn)值 必填 說明
offset nubmer 引用的服務(wù)端時間偏移量,毫秒為單位,可以是正數(shù)或負(fù)數(shù)

返回值 ServerDate

示例代碼

新增記錄時設(shè)置字段為服務(wù)端時間:

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

更新字段為服務(wù)端時間往后一小時:

db.collection('todos').doc('my-todo-id').update({
  due: db.serverDate({
    offset: 60 * 60 * 1000
  })
})
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號