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

SDK Database Collection build query criteria


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Collection.where(condition: Object): Collection

Support: small programs, cloud functions, web

Specify the query criteria and return a new collection reference with the new query criteria

Parameters

condition: Object

The query criteria

Returns a value

Collection

The sample code

const _ = db.command
const result = await db.collection('todos').where({
  price: _.lt(100)
}).get()


Collection.limit(value: number): Collection

Support: small programs, cloud functions, web

Specifies the maximum number of query result sets

Parameters

value: number

Returns a value

Collection

Description

Limit defaults and maximums 20 on small terminals and 1000 on cloud functions

The sample code

db.collection('todos').limit(10)
  .get()
  .then(console.log)
  .catch(console.error)

Collection.orderBy(fieldPath: string, string: order): Collection

Support: small programs, cloud functions, web

Specify the query sort criteria

Parameters

fieldPath: string

string: order

Returns a value

Collection

Description

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

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):

db.collection('todos')
  .orderBy('progress', 'desc')
  .orderBy('description', 'asc')
  .get()
  .then(console.log)
  .catch(console.error)

Collection.skip(offset: number): Collection

Support: small programs, cloud functions, web

When a specified query returns results, it starts with the results of the specified sequence and is often used for plying

Parameters

offset: number

Returns a value

Collection

The sample code

db.collection('todos').skip(10)
  .get()
  .then(console.log)
  .catch(console.error)

Collection.field(projection: Object): Collection

Support: small programs, cloud functions, web

Specifies the fields that need to be returned for the record in the return result

Parameters

projection: Object

Returns a value

Collection

Description

The method accepts a required object to specify the field to be returned, the individual keys of the object represent the fields to return or not to return, and value is passed in true|false (or 1|-1) to indicate whether to return or not to return.

The sample code

Only the description, done, and progress fields are returned:

db.collection('todos').field({
  description: true,
  done: true,
  progress: true,
})
  .get()
  .then(console.log)
  .catch(console.error)