Translate into your own language

Saturday, June 25, 2016

What is a database in MongoDB, how to create and use it

Database: In MongoDB, a databases hold collections of documents.

MongoDB don't provides any command to create “database“. Actually, you don’t need to create it manually, because, MangoDB will create it on the fly, during the first time you save the value into the collection.

The use Command

MongoDB use DATABASE_NAME is used to create database. The command will create a new database, if it doesn't exist otherwise it will return the existing database.

Syntax:

Basic syntax of use DATABASE statement is as follows:

>use DATABASE_NAME

Example:

If you want to create a database with name <mydb>, then use DATABASE statement would be as follows:

>use mydb
switched to db mydb

To check your currently selected database use the command db

>db
mydb

If you want to check your databases list, then use the command show dbs.

>show dbs
local     0.78125GB
test      0.23012GB

Your created database (mydb) is not present in list. To display database you need to insert atleast one document into it.

>db.movie.insert({"name":"tutorials point"})
>show dbs
local      0.78125GB
mydb       0.23012GB
test       0.23012GB

In mongodb default database is test. If you didn't create any database then collections will be stored in test database.

No comments:

Post a Comment