This topic describes how to create a database and collection in an ApsaraDB for MongoDB instance and write data to the collection.
Prerequisites
You have successfully connected to the instance by following steps in Getting Started.
Procedure
DMS
Create a database named
test.Enter the following command and click Execute.
use test
After the database is created, click the database name in Execution History to go to the database environment.
Create a collection named
mongoin thetestdatabase.Enter the following command and click Execute. If the Execution Confirmation dialog box appears, click OK.
db.createCollection("mongo")If the value of
okin the returned result is1.0, the collection is created. Other values indicate that the creation failed.
Write two sets of data
{"name": "test"}and{"count": "10"}to themongocollection.Enter the following command and click Execute. If the Execution Confirmation dialog box appears, click OK.
db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
Query data in the
mongocollection.Enter the following command and click Execute.
db.getCollection("mongo").find({})
MongoDB shell
Create a database named
testand switch to it.use test
Create a collection named
mongoin thetestdatabase.db.createCollection("mongo")When the value of
okin the returned result is1.0, the collection is created. Other values indicate that the creation failed.
Write two sets of data
{"name": "test"}and{"count": "10"}to themongocollection.db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
Query data in the
mongocollection.db.getCollection("mongo").find({})