Create new database in MongoDB

Today we’ll look at how to create a new database in MongoDB.

Connect to mongod via the following line, replace your credentials

mongo -u 'username' -p 'password' --authenticationDatabase 'admin'

If you’re successfully authenticated then you’ll see the following prompt:

mongo terminal prompt

To create a new database issue the following at this prompt:

use the-db-name-you-want-to-set

This is how you create a new database in MongoDB, if the database doesn’t exist then MongoDB will create the database and switch to it.

mongo create database terminal prompt

At this point, if you issue show databases at the prompt it won’t show the newly created database. You need to insert some data into it for it to stick, do this the following way:

db.new_collection.insert({ foo: "bar" })

That’s it, you’ve successfully created a new database in MongoDB.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.