This topic describes how to create databases and collections in an ApsaraDB for MongoDB instance and how to write data to the created collections in the Data Management (DMS) console.

Prerequisites

Procedure

  1. On the SQL Console page in the DMS console, run the following command to create a database named test:
    use test
  2. After the database is created, click the database name in the Execution History field.
  3. In the test database, run the following command to create a collection named mongo:
    db.createCollection("mongo")

    If a value of 1.0 is returned for ok, the collection is created. Otherwise, the collection failed to be created.

  4. Run the following command to write the following data to the mongo collection: {"name": "test"} and {"count": "10"}.
    db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
  5. Run the following command to query the data stored in the mongo collection:
    db.getCollection("mongo").find({})
    The following result is returned:
    [
        {
            '_id': ObjectId("63bd29f8e52fddefeb59****"),
            'name': "test"
        },    {
            '_id': ObjectId("63bd29f8e52fddefeb59****"),
            'count': "10"
        }
    ]