CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > MongoDB useful commands

MongoDB useful commands

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: January 16, 2022 · 2 min read · 0 Comments
Share: X in 🔗
check MongoDb database size

MongoDB, a popular NoSQL database, empowers developers with flexibility and scalability. Whether you’re a beginner or an experienced user, mastering MongoDB useful commands is crucial for efficient data manipulation. In this guide, we’ll dive into essential commands, covering everything from querying documents to managing user authentication.

Create user in MongoDB

  1. Connect mongodb by typing mongo
  2. Switch to admin database by typing use admin
  3. Create user and password with the appropriate role. Like following:
db.createUser(
  {
    user: "admin",
    pwd: "admin",
    roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
  }
)
  1. Then disconnect from the MongoDb shell by typing Ctl+D

Enable authentication in MongoDB

  1. Open /etc/mongod.conf with your favorite editor like vim. Eg. sudo vim /etc/mongod.conf
  2. Now enable the authorization. You may find the following:
security:
  authorization: "disabled"

And change it to 

security:
  authorization: "enabled"
  1. Save and restart the MongoDB using sudo service mongodb restart
  2. Now you can connect to MongoDB using the following:

If you are using mongosh client then replace mongo with mongosh.

mongo mongodb://localhost:27017 -u codersathi -p

Note: You may create other users for a specific database with a specific role

Example:

use test
db.createUser(
  {
    user: "testuser",
    pwd: "testpassword",
    roles: [ { role: "readWrite", db: "test" } ]
  }
)

This means, creating a testuser with testpassword and readWrite role for test database.

List all users from MongoDB database

  1. Go to the database by typing: use db_name
  2. Query like: db.getUsers()

Change user password in MongoDB

db.changeUserPassword("username_to_change_pwd", passwordPrompt())

Login with username and password in MongoDB

Command:

mongo database_name -u username -p

Enable MongoDB Remote Access

  1. To enable remote access to mongodb we need to send the 0.0.0.0 value for bindIp in config /etc/mongod.conf

Change User Role in  MongoDB

db.updateUser( "peg",
{
    roles: [{role: "dbOwner", db : "peg"} ]
})

Login from remote machine

mongo myhost.com -u username -p --authenticationDatabase authenticationDAtabaseName

List collections

show collections

Create collections

db.createCollection(‘collectionname’)

Rename collections

db.oldCollectionName.renameCollection(‘newCollectionName’)

Insert data in the collection

db.collectionName.insert({‘id’:1, ‘name’:’Virat’}) // Or we can pass any json object db.collectionName.insert(jsonObject)

Drop/Delete database

use dbname // Now delete the database using following query db.dropDatabase()

Warning!

Please use this command wisely. Once the database is deleted you can’t revert it back.

Related Posts:

  • Connect MongoDB Atlas Cluster Using Shell
  • Switch Statement with Strings in Java
  • Control Statements in Java
  • Install MongoDB BI Connector on Ubuntu
  • How to Write User Stories and Acceptance Criteria: A…
  • Array Operator in MongoDB
Tags:devopsmongodb
Was this article helpful?
← Previous ArticleVariable in Java
Next Article →Hashtable in Java

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap