How to Create and Drop database in MongoDB

1. Create Database in MongoDB

It’s strange to listen but true that MongoDB doesn’t provide any command to create databases. Then the question is how would we create database ?. The answer is – We don’t create database in MongoDB, we just need to use database with your preferred name and need to save a single record in database to create it.

List Databases – First check the current databases in our system.

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

Use New Database – 
Now if we want to create database with name exampledb. Just run following command and save a single record in database. After saving your first example, you will see that 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 the new database will be their with name exampledb.

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

2. Drop Database in MongoDB

MongoDB provides dropDatabase() command to drop currently used database with their associated data files. Before deleting make sure what database are you selected using db command.

> db
exampledb

Now if you execute dropDatabase() command. It will remove exampledb database.

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

To delete MongoDB database from Linux command line or Shell Script – Use following command from

# mongo exampledb --eval "db.dropDatabase()"

转载于:https://my.oschina.net/liting/blog/1982066

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值