Usage of WeChat Mini Program Cloud Database Collection/Query .orderBy

Collection.orderBy / Query.orderBy

Specify query sort criteria

The method signature is as follows:

function orderBy(fieldName: string, order: string): Collection | Query

The method accepts a required string parameter fieldNameto define the field to be sorted, and a string parameter to orderdefine the sort order. orderCan only take ascor desc.

If you need to sort the nested fields, you need to use "dot notation" to connect the nested fields, such as style.colorrepresenting stylethe nested fields in the field color.

At the same time, it also supports sorting by multiple fields, which orderBycan be called multiple times . The order orderByof multiple field sorting will sort multiple fields according to the calling order.

Sample code:

  • Sort by a field : get to-do items in ascending order by progress
const db = wx.cloud.database()
db.collection('todos').orderBy('progress', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)
  • Sort by multiple fields :
    first sort by progressdescending order (the progressbigger the higher the front), and then press descriptionthe ascending order (the alphabetical order the higher the front) to take the to-do list:
const db = wx.cloud.database()
db.collection('todos')
  .orderBy('progress', 'desc')
  .orderBy('description', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)

Original: WeChat Official Document · Mini Program -> Cloud Development -> Database -> Collection.orderBy / Query.orderBy

Guess you like

Origin blog.csdn.net/write_1m_lines/article/details/104170538