MongoDB

MongoDB Commands Cheat Sheet for Beginners

In this post, you will learn about MongoDB commands which could get you started and perform minimum database related activities such as create, update, drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as the cheat sheet. You may want to bookmark this page for quick reference.


MongoDB Commands Cheatsheet

The following is the list of the commands:

    • Start and stop the MongoDB Database
      sudo service mongod start
      sudo service mongod stop
      
    • Access the MongoDB database using Shell
      mongo --host localhost:27017
      
    • Show all databases
      show dbs
      
    • Create a database, say, testdb; Switch to the database
      use testdb
      

      Until a collection is created in a database, the database name is not listed as a result of execution of the command, “show dbs”

    • Add a collection
      db.createCollection("user")
      
    • Show all collections in a database; Execute the “use dbname” command to access the database before executing the command given below.
      show collections
      show tables
      

      The following command also work:

      db.getCollectionNames()
      
    • Insert a record in the collection; A record is inserted in the collection, “user”.
      db.user.insert({"name": "Ajitesh Shukla", "location": "hyderabad", "username": "eajitesh"})
      
    • Display list of records of a collection; “user” collection is used.
      db.user.find()
      db.user.find().pretty()
      
    • Display a list of records matching with value (s) of specific fields
      db.user.find({"username": "eajitesh"})
      db.user.find({"username": "eajitesh", "location": "hyderabad"})
      
  • Drop the collection
    db.user.drop()
    
  • Create users in the database; The below command creates a user with username as “ajitesh” and having the role such as “readWrite” and “dbAdmin”
    db.createUser({"user": "ajitesh", "pwd": "gurukul", "roles": ["readWrite", "dbAdmin"]})
    
  • Show users; If executed without selecting a database, it displays all users along with database information.
    show users
    
  • Login into the database with username and password
    mongo -u USERNAME -p PASSWORD --authenticationDatabase DATABASENAME
    

    For user created in above command, the login command would look like the following:

    mongo -u ajitesh -p gurukul --authenticationDatabase testdb
    

References

Summary

In this post, you learned about the MongoDB cheatsheet commands (especially, helpful for beginners) which could help you quickly get started with MongoDB.

Did you find this article useful? Do you have any questions or suggestions about this article? Leave a comment and ask your questions and I shall do my best to address your queries.

Ajitesh Kumar

I have been recently working in the area of Data analytics including Data Science and Machine Learning / Deep Learning. I am also passionate about different technologies including programming languages such as Java/JEE, Javascript, Python, R, Julia, etc, and technologies such as Blockchain, mobile computing, cloud-native technologies, application security, cloud computing platforms, big data, etc. I would love to connect with you on Linkedin. Check out my latest book titled as First Principles Thinking: Building winning products using first principles thinking.

Share
Published by
Ajitesh Kumar
Tags: mongodbnosql

Recent Posts

Agentic Reasoning Design Patterns in AI: Examples

In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…

1 month ago

LLMs for Adaptive Learning & Personalized Education

Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…

1 month ago

Sparse Mixture of Experts (MoE) Models: Examples

With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…

2 months ago

Anxiety Disorder Detection & Machine Learning Techniques

Anxiety is a common mental health condition that affects millions of people around the world.…

2 months ago

Confounder Features & Machine Learning Models: Examples

In machine learning, confounder features or variables can significantly affect the accuracy and validity of…

2 months ago

Credit Card Fraud Detection & Machine Learning

Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…

2 months ago