codecamp
QQ小程序 Collection

orderBy

Collection.orderBy(fieldPath: string,order: string): Collection

指定查询排序条件

参数

参数名 类型
fieldPath string
order string

返回值

Collection

说明

方法接受一个必填字符串参数 fieldName 用于定义需要排序的字段,一个字符串参数 order 定义排序顺序。order 只能取 ascdesc。 如果需要对嵌套字段排序,需要用 "点表示法" 连接嵌套字段,比如 style.color 表示字段 style 里的嵌套字段 color。 同时也支持按多个字段排序,多次调用orderBy即可,多字段排序时的顺序会按照 orderBy调用顺序先后对多个字段排序

示例代码:按一个字段排序

按进度排升序取待办事项

db.collection("todos")
  .orderBy("progress", "asc")
  .get()
  .then(console.log)
  .catch(console.error);

示例代码:按多个字段排序

先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待办事项:

db.collection("todos")
  .orderBy("progress", "desc")
  .orderBy("description", "asc")
  .get()
  .then(console.log)
  .catch(console.error);

remove

Collection.remove(): Promise<Object>

删除多条记录。注意只支持通过匹配 where 语句来删除,不支持 skip 和 limit。

返回值

Promise.<Object>

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

stats 的结构

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

注意事项

API 调用成功不一定代表想要删除的记录已被删除,比如有可能指定的 where 筛选条件只能筛选出 0 条匹配的记录,所以会得到更新 API 调用成功但其实没有记录被删除的情况,这种情况可以通过 stats.removed 看出来

示例代码

删除字段a的值大于2的文档

collection.where({
  a: db.command.gt(2)
}).remove().then(function(res) {

skip

Collection.skip(offset: number): Collection

指定查询返回结果时从指定序列后的结果开始返回,常用于分页

参数

offset: number

返回值

Collection

示例代码

db.collection('todos').skip(10)
  .get()
  .then(console.log)
  .catch(console.error)

updata

Collection.update(): Promise<Object>

更新多条记录

返回值

Promise.<Object>

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

stats 的结构

|属性|类型|说明| |updated|number|成功更新的记录数量|

注意事项

API 调用成功不一定代表想要更新的记录已被更新,比如有可能指定的 where 筛选条件只能筛选出 0 条匹配的记录,所以会得到更新 API 调用成功但其实没有记录被更新的情况,这种情况可以通过 stats.updated 看出来

示例代码

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

db.collection("todos")
  .where({
    done: false
  })
  .update({
    data: {
      progress: _.inc(10)
    }
  })
  .then(console.log)
  .catch(console.error);

Collection.where(condition: Object): Collection

指定查询条件,返回带新查询条件的新的集合引用

参数

condition: Object 查询条件

返回值

Collection

示例代码

const _ = db.command
const result = await db.collection('todos').where({
  price: _.lt(100)
}).get()
QQ小程序 Collection
QQ小程序 Command
温馨提示
下载编程狮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; }