# Database API

Mini Program Cloud Base provides a rich set of database APIs. Here is the API reference documentation for the database Server. It can be used in a cloud function environment.

Server APIs are almost the same as the Mini Program APIs except for the followings:

  1. Server APIs no longer accept callback (success, fail, and complete) but all return Promise.
  2. The Server has bulk write and bulk delete permissions. This means that update or remove can be called in a collection or query statement.
  3. Unique server APIs, such as the API for collection creation (db.createCollection).

Database APIs use lazy execution, which means that only API calls that actually require a network request can initiate a network request, and other calls, such as getting a database, collection, record reference, and constructing a query condition on a collection, cannot trigger a network request. The APIs that trigger network requests are as follows:

API Description
get Gets data collection/record data
add Adds a record to the collection
update Updates data collection/record data
set Replaces and updates a record
remove Deletes record
count Counts the number of records matched to the query statement

The APIs to get reference are as follows:

API Description
database Gets a database reference, and the Database object is returned
collection Gets a collection reference, and the Collection object is returned
doc Gets a record reference, and the Document object is returned

The "Database" object has the following fields:

Field Description
command Gets database query and update commands, and Command is returned
serverDate Constructs a server time
Geo Gets a geographical location operation object, and the Geo object is returned
createCollection Create a collection

The "Collection" object has the following APIs:

API Description
doc Gets a record reference, and the Document object is returned
add Adds a record to the collection
update Update data
where Constructs a query condition on the current collection, and Query is returned. Query commands can be used in the query condition.
remove Delete records that match the filter criteria
orderBy Specifies the sort order of query data
limit Specifies the maximum amount of return data
skip Specifies the item in the matched record list after which the result is returned when you perform a query
field Specifies the fields that should be included in each record in the return result

The "Document" object has the following APIs:

API Description
get Gets record data
update Locally updates data
set Replaces and updates record
remove Deletes record
field Specifies the fields that should be included in the record of the return result

The Command (db.command) object has the following query commands:

API Description
eq Indicates whether the field value is equal to the specified value
neq Indicates whether the field value is not equal to the specified value
lt Indicates whether the field value is less than the specified value
lte Indicates whether the field value is less than or equal to the specified value
gt Indicates whether the field value is greater than the specified value
gte Indicates whether the field value is greater than or equal to the specified value
in Indicates whether the field value is in the specified array
nin Indicates whether the field value is in the specified array
and The condition "and" means that another condition must be met at the same time
or The condition "or" means that another condition may be met

The Command (db.command) object has the following update commands:

API Description
set Sets the field as specified value
remove Deletes field
inc Atomic auto-incrementing field value
mul Atomic auto-multiplying field value
push If the field value is an array, add a specified value to the end of the array
pop If the field value is an array, delete an element from the end of the array
shift If the field value is an array, delete an element from the head of the array
unshift If the field value is an array, add the specified value to the head of the array

The object of Error returned from API reject has the following two fields.

Field Type Description
errCode number Error Codes
errMsg string Error message
点击咨询小助手