Big Data and Hadoop (170 Blogs) Become a Certified Professional
AWS Global Infrastructure

Big Data

Topics Covered
  • Big Data and Hadoop (146 Blogs)
  • Hadoop Administration (8 Blogs)
  • Apache Storm (4 Blogs)
  • Apache Spark and Scala (29 Blogs)

How To Create User In MongoDB?

Last updated on Mar 15,2023 3.5K Views

How To Create User In MongoDB?

If you have been in the database management and programming industry for sometime now, you have probably heard about MongoDB. First introduced in 2009, MongoDB is currently one of the most popular relational database management software available in the market today. If you are new to using MongoDB, one of the first things you need to do is to create a user in MongoDB and then create a database likewise. In this article we will discuss about both.

Following pointers will be covered in this article,

Let’s get started.

How To Create A User In MongoDB?

In order to create a user in MongoDb, you need to make use of the createuser function. Take a look at the example shared below to understand how this is done.

user: "Edureka",
pwd: "password",
roles:[{role: "userAdminAnyDatabase" , db:"admin"}]})


Let us breakdown the code shared above.

  • Mention the username and the password that will be used in the system.
  • Allot a role to the user we just created. Here, we have given the user the role of an administrator and thus used the method, userAdminAnyDatabase. What this means is that, this user will have the administrative accessibility to the entire databases that are present as well as those that will be created in the MongoDB architecture from now on.
  • We have made use of the parametre db to specify the database of the admin. This is a special Meta database inside the mongoDb architecture which contains all the details of the user we just created.

When the command is performed correctly, the output will look something like this.

Output - Create User In MongoDB - EdurekaThus we have successfully created a user in MongoDB

Now let us see how to create a database in MongoDB

Create A Database In MongoDB

One of the most important things to note while creating a database in MongoDb is that, there is no special command or module that can be used to achieve this. While this might look strange if you have started using MongoDB after using other SQL database management systems where you need to create a database, create a table and then assign values within the same.

But in MongoDb, the database is automatically created by the system without any interference from the end user.

Another important thing to note here is that, you don’t need to specify what type of data you will be storing inside the database. The same will be automatically assigned once you have inputted the data into the created database.

Now that you know the basics, let us see how to create a database in MongoDb if none has been created automatically or you need to create an entirely new one.

In order to do this, you need to use the syntax,


If the database you just entered already exists in the architecture, then you will be automatically switched and will start using the same. To understand this better, take a look at the example below.

>use javatpointdb

The output will look like this

Swithched to db javatpointdb

If you want to see the database you are currently using, then use this command.


If you want to view a list of the entire databases that exist within the architecture, use this command.

>show dbs

If you want to insert a new list into a database that already exists then make use of this command.


The above is an example and the parameters can be changed depending upon your needs.

How To Create A User For A Single Database?

If in a certain situation, you want to develop a user for a single database you need to make use of the userAdmin module.

Output - Create User In MongoDB - EdurekaAs you can see in the image above, we have made use of the following code.

user: "Employeeadmin",
pwd: "password",
roles:[{role: "userAdmin" , db:"Employee"}]})


  • Mention the password of the user that you want to create.
  • To the recently created user, you must allot a role. We will make use of the userAdmin module to assign the role of an Admin to this user.
  • Finally we have made use of the parametre db to specify which database this user has privileges to.

When the above code is run successfully, the output will look something like this.

Output - Create User In Monddb - EdurekaThe above example shows the creation of a user name – EmployeeAdmin and that it has permission to use the database Employee.

This brings us to the end of this article on Create User In MongoDB.

For details, You can even check out tools and systems used by Big Data experts and its concepts with the Data engineer online course.

Now that you have understood what is Big Data, check out the Big Data training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. The Edureka Big Data Hadoop Certification Training course helps learners become expert in HDFS, Yarn, MapReduce, Pig, Hive, HBase, Oozie, Flume and Sqoop using real-time use cases on Retail, Social Media, Aviation, Tourism, Finance domain.

Got a question for us? Mention them in the comments section and we will get back to you. 


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How To Create User In MongoDB?