微信小程序 云数据库 Collection/Query .orderBy 用法

Collection.orderBy / Query.orderBy

指定查询排序条件

方法签名如下:

function orderBy(fieldName: string, order: string): Collection | Query

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

如果需要对嵌套字段排序,需要用 “点表示法” 连接嵌套字段,比如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)

原文:微信官方文档 · 小程序 -> 云开发 -> 数据库 -> Collection.orderBy / Query.orderBy

猜你喜欢

转载自blog.csdn.net/write_1m_lines/article/details/104170538