# Collection.add
Adds a record to the collection.
Function signature is shown as below:
function add(options: object): Promise<Result>
Parameter description
Field | Type | Required | Default | Description |
---|---|---|---|---|
data | Object | Yes | Definition of new record |
Description of return value
The results of resolve
and reject
for Promise
are defined as below:
Result Description | |
---|---|
resolve | The result of a new record. The Result is defined as below. |
reject | Reason for failure |
Result description
Result
from resolve
is an object with the following structure:
Field | Type | Description |
---|---|---|
_id | String | Number | The ID of a new record |
Sample code
Add a new to-do:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').add({
// The data field represents the JSON data to add
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
// Location (113°E, 23°N)
location: new db.Geo.Point(113, 23),
done: false
}
})
} catch(e) {
console.error(e)
}
}