W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
指定查詢排序條件
方法簽名如下:
function orderBy(fieldName: string, order: string): Collection | Query
方法接受一個(gè)必填字符串參數(shù) fieldName 用于定義需要排序的字段,一個(gè)字符串參數(shù) order 定義排序順序。order 只能取 asc 或 desc。
如果需要對(duì)嵌套字段排序,需要用 "點(diǎn)表示法" 連接嵌套字段,比如 style.color 表示字段 style 里的嵌套字段 color。
同時(shí)也支持按多個(gè)字段排序,多次調(diào)用 orderBy 即可,多字段排序時(shí)的順序會(huì)按照 orderBy 調(diào)用順序先后對(duì)多個(gè)字段排序
示例代碼:按一個(gè)字段排序
按進(jìn)度排升序取待辦事項(xiàng)
const db = wx.cloud.database()
db.collection('todos').orderBy('progress', 'asc')
.get()
.then(console.log)
.catch(console.error)
示例代碼:按多個(gè)字段排序
先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待辦事項(xiàng):
const db = wx.cloud.database()
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
.then(console.log)
.catch(console.error)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: