Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program cloud development API specifies query sorting criteria


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Collection.orderBy / Query.orderBy

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.

If you need to sort nested fields, you need to connect nested fields with a "point notation", such as style.color, to represent the nested field color in the field style.

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 db = wx.cloud.database()
db.collection('todos').orderBy('progress', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)

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 db = wx.cloud.database()
db.collection('todos')
  .orderBy('progress', 'desc')
  .orderBy('description', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)