DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

UK-US Data Bridge: Join TechnologyAdvice and OneTrust as they discuss the UK extension to the EU-US Data Privacy Framework (DPF).

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • Are You Facing an Error When You Run the StartDagServerMaintenance.ps1 Script?
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • SQL Query Performance Tuning in MySQL
  • How to Move System Databases to Different Locations in SQL Server on Linux

Trending

  • What’s New Between Java 17 and Java 21?
  • Legacy App Migration: How Discovery Phase Speeds up Project Delivery
  • Instant Microservices: Rules for Logic and Security
  • Demystifying Event Storming: A Comprehensive Guide to Understanding Complex Systems (Part 1)
  1. DZone
  2. Data Engineering
  3. Databases
  4. MongoDB Commands Cheat Sheet for Beginners

MongoDB Commands Cheat Sheet for Beginners

New to MongoDB? Then this command cheat sheet will come in handy. Even if you aren't a Mongo rookie, this sheet may still be useful to you!

Ajitesh Kumar user avatar by
Ajitesh Kumar
·
Jul. 09, 18 · Tutorial
Like (8)
Save
Tweet
Share
82.3K Views

Join the DZone community and get the full member experience.

Join For Free

In this post, you will learn about MongoDB commands, which could get you started and perform minimum database related activities such as create, update, and drop a collection (table). These commands are ideally meant for MongoDB beginners and could be taken as a 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

MongoDB Shell Quick Reference

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.



If you enjoyed this article and want to learn more about MongoDB, check out this collection of tutorials and articles on all things MongoDB.

Command (computing) MongoDB Database

Published at DZone with permission of Ajitesh Kumar, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Are You Facing an Error When You Run the StartDagServerMaintenance.ps1 Script?
  • Java and MongoDB Integration: A CRUD Tutorial [Video Tutorial]
  • SQL Query Performance Tuning in MySQL
  • How to Move System Databases to Different Locations in SQL Server on Linux

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: