Home  >  Article  >  Database  >  How to create and delete database in MongoDB

How to create and delete database in MongoDB

不言
不言Original
2019-03-23 15:00:454068browse

This article will introduce to you how to create and delete a database in MongoDB. Let’s take a look at the specific operation content.

How to create and delete database in MongoDB

#1. Create a database in MongoDB

List databases: First check the current database in the system.

First of all, we should know that MongoDB does not provide any command to create a database. So how will we create the database? The answer is we don't create a database in MongoDB, we just create it using a database with the name you need and save a single record in the database.

# mongo
> show dbs;
admin  (empty)
local  0.078GB
test   0.078GB

Using a new database:

Now, if we want to create a database named exampledb. Just run the following command and save a record in the database. After saving the first example, you will see that the new database has been created.

> use exampledb;
> s = { Name : "TecAdmin.net" }
> db.testData.insert( s );

List databases:

Now, if you list the databases, you will see that the new database will be the one with the name exampledb.

> show dbs;
admin  (empty)
local  0.078GB
exampledb   0.078GB
test   0.078GB

2. Delete the database in MongoDB

MongoDB provides the dropDatabase() command to delete the currently used database and its associated data files. Before deleting, make sure which database is selected using the db command.

> db
exampledb

Now, if you execute the dropdatabase() command. It will delete the ExampleDB database.

> db.dropDatabase();
{ "dropped" : "exampledb", "ok" : 1 }

To delete the MongoDB database from the Linux command line or shell script, please use the following command

# mongoexampledb--eval "db.dropDatabase()"

This article is all over here, for more other exciting content, you can follow PHP Chinese website's MySQL video tutorial column!

The above is the detailed content of How to create and delete database in MongoDB. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn