All Products
Search
Document Center

ApsaraDB for MongoDB:Write data

Last Updated:Jun 21, 2026

This topic describes how to create a database and a collection in an ApsaraDB for MongoDB instance and write data to the collection.

Prerequisites

You have connected to the instance by following the quick start steps.

Procedure

DMS

  1. Create a database named test.

    Run the following command and click Execute.

    use test

    After running the command, the result pane displays the following message: The current switch is invalid. Open a new test tab to proceed.

    After the database is created, click the database name in the Execution Result tab to switch to that database.

  2. In the test database, create a collection named mongo.

    Run the following command and click Execute. If the Execution Confirmation dialog box appears, click Confirm.

    db.createCollection("mongo")

    If the ok field in the result is 1.0, the collection is created. Other values indicate failure.

  3. Insert two documents, {"name": "test"} and {"count": "10"}, into the mongo collection.

    Run the following command and click Execute. If the Execution Confirmation dialog box appears, click Confirm.

    db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})

    After the command runs, the result shows an nInserted value of 2, indicating that two documents were inserted.

  4. Query the documents in the mongo collection.

    Run the following command and click Execute.

    db.getCollection("mongo").find({})

    After the command runs, the JSON view displays the two returned documents: one containing name: "test" and the other containing count: "10".

MongoDB shell

  1. Create and switch to the test database.

    use test
  2. In the test database, create a collection named mongo.

    db.createCollection("mongo")

    If the ok field in the result is 1.0, the collection is created. Other values indicate failure.

  3. Insert two documents, {"name": "test"} and {"count": "10"}, into the mongo collection.

    db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
    mgset-80xxx [primary] test> db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
    {
      n: 2,
      electionId: ObjectId('7fffffff0000000000000001'),
      opTime: { ts: Timestamp({ t: 1747708770, i: 1 }), t: Long('1') },
      ok: 1,
      '$clusterTime': {
        clusterTime: Timestamp({ t: 1747708770, i: 1 }),
        signature: {
          hash: Binary.createFromBase64('xDyGfZsHUuJXLQE5vgX18karcl0=', 0),
          keyId: Long('7506346035782877189')
        }
      },
      operationTime: Timestamp({ t: 1747708770, i: 1 })
    }
  4. Query the documents in the mongo collection.

    db.getCollection("mongo").find({})
    mgset-xxxxxxxx [primary] test> db.getCollection("mongo").find({})
    [
      { _id: ObjectId('682beb626f94807446850cc1'), name: 'test' },
      { _id: ObjectId('682beb626f94807446850cc2'), count: '10' }
    ]