W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
指定查詢排序條件
方法簽名如下:
function orderBy(fieldName: string, order: string): Collection | Query
方法接受一個必填字符串參數(shù) fieldName 用于定義需要排序的字段,一個字符串參數(shù) order 定義排序順序。order 只能取 asc 或 desc。
同時也支持按多個字段排序,多次調(diào)用 orderBy 即可,多字段排序時的順序會按照 orderBy 調(diào)用順序先后對多個字段排序
示例代碼:按一個字段排序
按進度排升序取待辦事項
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()
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: