Add records

更新时间:
复制 MD 格式

Each record in the database is a JSON object. You can add records directly in the console.

Prerequisites

Create a data table

Operations in the console

  1. Log on to the EMAS console. Select Serverless, and then click Enter to open the Serverless console.

  2. In the navigation pane on the left, select Cloud Database.

  3. On the Cloud Database page, click the target data table, and then click Add Record.

  4. On the Add Record page, enter the record content in JSON format, and then click OK.

    Each record has a unique ID. After you add a record, you can delete or modify it.

Client calls

  • Add a single record.

    mpserverless.db.collection('users').insertOne({
        name: 'tom',
        age: 1
    })
    .then(res => {})
    .catch(console.error);
  • Add multiple records.

    mpserverless.db.collection('users').insertMany([{
        name: 'tom',
        age: 1
    },{
        name: 'jerry',
        age: 2
    }])
    .then(res => {})
    .catch(console.error);

Cloud function invocations

  • Add a single record.

    'use strict';
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users').insertOne({
         name: 'tom',
          age: 1
      });
    };
  • Add multiple records.

    'use strict';
    module.exports = async function (ctx) {
      return await ctx.mpserverless.db.collection('users').insertMany([{
        name: 'tom',
        age: 1
      },
      {
        name: 'jerry',
        age: 2
      }]);
    };