May 20, 2021 WeChat Mini Program Development Document
Specify the query sort criteria
The method signature is as follows:
function orderBy(fieldName: string, order: string): Collection | Query
The method accepts a required string argument fieldName to define the fields that need to be sorted, and a string parameter order defines the sort order. Order can only take asc or desc.
It also supports sorting by multiple fields, just call orderBy multiple times, and sort multiple fields in order of orderBy calls
Example code: Sort by field
Prioritize to-dos by schedule
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()
}
Example code: Sorted by multiple fields
Take the to-dos first in progress descending order (the bigger the progress, the higher the front), and then in the description ascending order (the higher the alphabetical order, the higher the forward):
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()
}