# Database API

Mini Program Cloud Base provides a rich set of database APIs. Here is the API documentation for the database of Mini Program.

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

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
where Constructs a query condition on the current collection, and Query is returned. Query commands can be used in the query condition.
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