新聞中心
Collection.where(condition: Object): Collection
支持端:小程序 , 云函數 , Web

支持端:小程序 , 云函數 , Web

指定查詢條件,返回帶新查詢條件的新的集合引用
參數
condition: Object
查詢條件
返回值
Collection
示例代碼
const _ = db.command
const result = await db.collection('todos').where({
price: _.lt(100)
}).get()
const _ = db.command
const result = await db.collection('todos').where({
price: _.lt(100)
}).get()Collection.limit(value: number): Collection
支持端:小程序 , 云函數 , Web
指定查詢結果集數量上限
參數
value: number
返回值
Collection
說明
limit 在小程序端默認及最大上限為 20,在云函數端默認及最大上限為 1000
示例代碼
db.collection('todos').limit(10)
.get()
.then(console.log)
.catch(console.error)
Collection.orderBy(fieldPath: string, string: order): Collection
支持端:小程序 , 云函數 , Web
指定查詢排序條件
參數
fieldPath: string
string: order
返回值
Collection
說明
方法接受一個必填字符串參數 fieldName 用于定義需要排序的字段,一個字符串參數 order 定義排序順序。order 只能取 asc 或 desc。
如果需要對嵌套字段排序,需要用 "點表示法" 連接嵌套字段,比如 style.color 表示字段 style 里的嵌套字段 color。
同時也支持按多個字段排序,多次調用 orderBy 即可,多字段排序時的順序會按照 orderBy 調用順序先后對多個字段排序
示例代碼:按一個字段排序
按進度排升序取待辦事項
db.collection('todos').orderBy('progress', 'asc')
.get()
.then(console.log)
.catch(console.error)
示例代碼:按多個字段排序
先按 progress 排降序(progress 越大越靠前)、再按 description 排升序(字母序越前越靠前)取待辦事項:
db.collection('todos')
.orderBy('progress', 'desc')
.orderBy('description', 'asc')
.get()
.then(console.log)
.catch(console.error)
Collection.skip(offset: number): Collection
支持端:小程序 , 云函數 , Web
指定查詢返回結果時從指定序列后的結果開始返回,常用于分頁
參數
offset: number
返回值
Collection
示例代碼
db.collection('todos').skip(10)
.get()
.then(console.log)
.catch(console.error)
Collection.field(projection: Object): Collection
支持端:小程序 , 云函數 , Web
指定返回結果中記錄需返回的字段
參數
projection: Object
返回值
Collection
說明
方法接受一個必填對象用于指定需返回的字段,對象的各個 key 表示要返回或不要返回的字段,value 傳入 true|false(或 1|-1)表示要返回還是不要返回。
示例代碼
只返回 description, done 和 progress 三個字段:
db.collection('todos').field({
description: true,
done: true,
progress: true,
})
.get()
.then(console.log)
.catch(console.error) 本文標題:創(chuàng)新互聯(lián)小程序教程:SDK數據庫Collection·構建查詢條件
瀏覽地址:http://fisionsoft.com.cn/article/djoeiso.html


咨詢
建站咨詢
