MongoDB is the most popular open-source No-SQL database. It is a document-based database that saves the data into a JSON document. In this blog post, I will explain MongoDB create database command and how we can create a database in mongodb using other methods. So let’s get started.
How to create a database in mongodb
Mongodb saves the data into mongo databases, and a single database can hold multiple collections.
Before creating a database in MongoDB, let’s verify what all database is initially present in mongodb by typing the below command
show dbs; admin 0.000GB config 0.000GB local 0.000GB
To create a database in mongodb type, use the command followed by <databasename>
use <databasename>
let’s use the above command and create a database named naiveskill
use naiveskill
let’s type the below command to verify the current database in which the user is:
> db naiveskill
Now let’s again run the show dbs command and verify if the new database is available
show dbs; admin 0.000GB config 0.000GB local 0.000GB
You might be wondering why the database is not displaying.
Why show dbs does not show the database in mongodb
The mongodb show dbs command will not show the databases that do not have any documents. Once you create a document for a collection under a particular database, then only the database will be visible.
Now let’s create a collection in the naiveskill database by typing the below command.
db.createCollection('test')
Verify the collection by typing the below command
show collections test
Let’s insert a document into the mongo test collection
db.test.insert({'Kind': 'Mobile', 'brand': 'iphone 6s', 'Rating': 4.2 })
Now again, let’s run the show dbs command and verify if the database is visible now
show dbs; admin 0.000GB config 0.000GB local 0.000GB naiveskill 0.000GB
Excellent, now the database is visible. If can follow this tutorial on mongodb commands to learn more about the mongodb.
Create database in mongodb compass
MongoDB Compass is a UI-based tool for querying, aggregating and analyzing your MongoDB data. Compass is open-source and free to use and can be run on macOS, Windows, and Linux.
You can download the mongodb from here.
Connect to mongodb UI by passing the mongodb connection string.
Once you are connected to the compass, you will get below the screen to create a database in mongodb.
Click on Create Database button and provide the database name to create a new database name naive.
Mongodb create a database with username and password
To create a database in mongodb using username and password, first, you need to connect to mongodb using username and password.
mongo --host “hostname:port” -u user -p password --authenticationDatabase admin test
Once the connection to MongoDB is successful, you can typically use <databasename> to create the database in mongodb.
use naiveskill
Mongodb create database script
We can interact with mongodb programmatically using many languages like C/C++, Java, Python, nodeJS, ruby, shift, scala, etc. In mongodb, this interaction happens via drivers. The complete list of drivers in mongodb can be found here.
One of the most popular drivers is the python driver, which uses the pymongo client to interact with python programmatically.
The pymongo driver can be installed by typing the below command:
pip install pymongo
Once the pymongo driver installation is complete, connect to the ipython/python shell and import pymongo
import pymongo
Now the next step is to create a MongoClient.The MongoClient takes the MongoDB connection string as a parameter.
Since, in my case, the MongoDB is running locally on port 27017, type the below connection string to connect to MongoDB.
client = pymongo.MongoClient("mongodb://localhost:27017/")
You can change the connecting string based on the server and port where mongodb is running.
Now the database can be programmatically created by using mongo client. Type the below command to create the ‘naiveskill’ database
db = client['naiveskill']
If you wish to learn more about the mongodb python client, you can follow this tutorial on MongoDB with python.
MongoDB create user
We can use mongodb createUser() command to create a user in mongodb
To create a user first, we need to create a database. Let’s create a new admin database
use admin
Now type the below command to create a user myUserAdmin and assign the role userAdminAnyDatabase
db.createUser( { user: "myUserAdmin", pwd: passwordPrompt(), // or cleartext password roles: [ { role: "userAdminAnyDatabase", db: "admin" }] } )
Now type the below command to verify if the user gets added to the admin database
use admin
Verify the user by typing the below command
show users
Conclusion
In this tutorial, we have learned various ways to create a database in mongodb, like using the command line, an atlas, and programmatically.
I hope you have liked this tutorial. Please do let me know in the comment box. I will be more than happy to hear from you.