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
mongo
in thetest
database.Enter the following command and click Execute. If the Execution Confirmation dialog box appears, click OK.
db.createCollection("mongo")
If the value of
ok
in 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 themongo
collection.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
mongo
collection.Enter the following command and click Execute.
db.getCollection("mongo").find({})
MongoDB shell
Create a database named
test
and switch to it.use test
Create a collection named
mongo
in thetest
database.db.createCollection("mongo")
When the value of
ok
in 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 themongo
collection.db.runCommand({insert: "mongo", documents: [{"name": "test"},{"count": "10"}]})
Query data in the
mongo
collection.db.getCollection("mongo").find({})