当前位置:K88软件开发文章中心编程全书微信小程序 → 文章内容

微信小程序云开发服务端数据库API 指定查询排序条件

减小字体 增大字体 作者:佚名  来源:网上搜集  发布时间:2019-1-15 15:03:19

由 Carrie 创建, 最后一次修改 2018-11-14 Collection.orderBy / Query.orderBy指定查询排序条件方法签名如下:function orderBy(fieldName: string, order: string): Collection | Query方法接受一个必填字符串参数 fieldName 用于定义需要排序的字段,一个字符串参数 order 定义排序顺序。order 只能取 asc 或 desc。同时也支持按多个字段排序,多次调用 orderBy 即可,多字段排序时的顺序会按照 orderBy 调用顺序先后对多个字段排序示例代码:按一个字段排序按进度排升序取待办事项const cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()exports.main = async (event, context) => { return await db.collection('todos').orderBy('progress', 'asc').get()}示例代码:按多个字段排序先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待办事项:const cloud = require('wx-server-sdk')cloud.init()const db = cloud.database()exports.main = async (event, context) => { return await db.collection('todos') .orderBy('progress', 'desc') .orderBy('description', 'asc') .get()}

微信小程序云开发服务端数据库API 指定查询排序条件