Create and delete a MongoDB Database
MongoDB database creation syntax
The syntax format for creating a MongoDB database is as follows:
use DATABASE_NAME
If the database does not exist, create a database. Otherwise, switch to the specified database.
Instance
The following instance creates the database runoob:
> use runoobswitched to db runoob> dbrunoob>
To view all databases, run the show dbs command:
> show dbslocal 0.078GBtest 0.078GB>
We can see that the created database runoob is not in the Database List. to display it, we need to insert some data to the runoob database.
> Db. runoob. insert ({"name": ""}) WriteResult ({"nInserted": 1})> show dbslocal 0.078 GBrunoob 0.078 GBtest 0.078 GB>
The default database in MongoDB is test. If you do not create a new database, the set will be stored in the test database.
MongoDB database deletion syntax
The syntax format for deleting a MongoDB database is as follows:
db.dropDatabase()
Delete the current database. The default value is test. You can use the db command to view the current database name.
Instance
The following instance deletes the database runoob.
First, view all databases:
> show dbslocal 0.078GBrunoob 0.078GBtest 0.078GB
Next we switch to the database runoob:
> use runoobswitched to db runoob>
Run the DELETE command:
> db.dropDatabase(){ "dropped" : "runoob", "ok" : 1 }
Finally, run the show dbs command to check whether the database is successfully deleted:
> show dbslocal 0.078GBtest 0.078GB>
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: