微信小程序开发 专题
专题目录
您的位置:微信小程序开发 > 微信小程序开发专题 > 微信小程序云开发API 指定查询排序条件
微信小程序云开发API 指定查询排序条件
作者:--    发布时间:2019-11-20

指定查询排序条件

方法签名如下:

function orderby(fieldname: string, order: string): collection | query

方法接受一个必填字符串参数 fieldname 用于定义需要排序的字段,一个字符串参数 order 定义排序顺序。order 只能取 asc 或 desc。

如果需要对嵌套字段排序,需要用 "点表示法" 连接嵌套字段,比如 style.color 表示字段 style 里的嵌套字段 color。

同时也支持按多个字段排序,多次调用 orderby 即可,多字段排序时的顺序会按照 orderby 调用顺序先后对多个字段排序

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

按进度排升序取待办事项

const db = wx.cloud.database()
db.collection('todos').orderby('progress', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)

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

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

const db = wx.cloud.database()
db.collection('todos')
  .orderby('progress', 'desc')
  .orderby('description', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册