Home > Database > Mysql Tutorial > How to create and delete database in MongoDB

How to create and delete database in MongoDB

不言
Release: 2019-03-23 15:00:45
Original
4100 people have browsed it

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;
Copy after login
admin  (empty)
local  0.078GB
test   0.078GB
Copy after login

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 );
Copy after login

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;
Copy after login
admin  (empty)
local  0.078GB
exampledb   0.078GB
test   0.078GB
Copy after login

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
Copy after login
exampledb
Copy after login

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

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

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

# mongoexampledb--eval "db.dropDatabase()"
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template