Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
MongoDB - Regex
Next article icon

MongoDB Shell

Last Updated : 27 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

MongoDB Shell is a powerful, interactive command-line interface that enables developers and database administrators to perform operations and manage their MongoDB databases using JavaScript syntax. Whether you're a beginner just starting with MongoDB or an experienced user, MongoDB Shell offers a comprehensive environment for executing queries, testing commands, and handling database tasks.

MongoDB Shell is a JavaScript-based tool that allows users to interact with MongoDB databases directly from the command line. It facilitates a wide range of operations from simple CRUD (Create, Read, Update, Delete) operations to more advanced database management tasks, making it an essential tool for anyone working with MongoDB.

MongoDB Shell allows developers to:

  • Perform CRUD operations
  • Query databases using rich syntax
  • Execute aggregation queries for data analysis
  • Handle large datasets efficiently
  • Interact with the database in real-time using an interactive interface

MongoDB Shell Interface

The MongoDB Shell provides a direct interface between users and their MongoDB databases, making it easier to perform a wide variety of tasks. It simplifies operations like:

  • Creating and deleting databases
  • Inserting and modifying documents
  • Running complex queries
  • Managing collections

The interface uses JavaScript syntax, which allows developers to take advantage of its flexibility and execute complex queries. The immediate feedback and command history support make it perfect for both beginners and advanced users.

Key Features of MongoDB Shell

MongoDB Shell comes packed with essential features that enhance the database management experience. Here are some key highlights:

  • Interactive Environment: MongoDB Shell offers an interactive experience where you can enter commands and see immediate results, making it ideal for learning and testing.
  • JavaScript-Based: Since the shell uses JavaScript, you can leverage its rich capabilities to manipulate data and execute complex queries.
  • Support for CRUD Operations: It perform all Create, Read, Update and Delete (CRUD) operations effortlessly through the shell.
  • Aggregation Framework: Access advanced data processing features using the aggregation framework.
  • Cross-Platform Compatibility: It use MongoDB Shell on various operating systems, including Windows, macOS and Linux.

Getting Started with MongoDB Shell

To begin using MongoDB Shell, simply install it on your system and launch it via the terminal or command prompt. Once connected, you can easily start interacting with your MongoDB databases using various commands. To start using MongoDB Shell, follow these easy steps:

1. Install MongoDB Shell:

  • Download the MongoDB Shell from the official MongoDB website.
  • Follow the installation guide for your operating system (Windows, macOS, or Linux).

2. Launch the Shell

  • Open your terminal or command prompt.
  • Type mongosh to start the MongoDB Shell.

3. Access Help

  • Type help within the shell to get a list of available commands and their descriptions.

Connecting to MongoDB using MongoDB Shell

To connect to a MongoDB deployment, you need a connection string. The connection string typically includes your MongoDB instance's address and authentication details.

For example, to connect to a MongoDB Atlas cluster, use the following command:

mongosh "mongodb+srv://mycluster.abcd1.mongodb.net/myFirstDatabase" --apiVersion 1 --username <username>

Once connected, you can start querying your database and performing various tas

Basic MongoDB Shell Commands

Let's explore some fundamental commands and operations you can perform using MongoDB Shell:

1. Show Databases: To view the list of databases, use the command:

show dbs

2. Switch Database: You can switch to a specific database using the use command:

use mydatabase

3. Show Collections: To list all collections within the current database, use:

show collections

4. Insert Document: To insert a document into a collection, use the insertOne() or insertMany() methods.

db.collection.insertOne({name: "Alice", age: 25})

5. Find Documents: To query documents in a collection:

db.collection.find()

Advanced MongoDB Shell Operations

As you become more proficient with MongoDB Shell, you can perform more advanced operations, such as:

1. Aggregation: MongoDB’s aggregation framework allows you to process data records and return computed results. For example:

db.orders.aggregate([
{ $match: { status: "completed" } },
{ $group: { _id: "$customer_id", total: { $sum: "$amount" } } }
])

2. Indexing: Indexes are used to improve query performance. You can create indexes in MongoDB Shell:

db.collection.createIndex({ name: 1 })

3. Updating Documents: Update one or more documents in a collection using updateOne() or updateMany():

db.collection.updateOne(
{ name: "Alice" },
{ $set: { age: 26 } }
)

4. Deleting Documents: To delete documents from a collection:

db.collection.deleteOne({ name: "Alice" })

Conclusion

In conclusion, MongoDB Shell is a powerful and versatile tool that provides developers and database administrators with an intuitive command-line interface to interact with MongoDB databases. Whether you're performing basic CRUD operations or leveraging advanced features like aggregation and indexing, MongoDB Shell simplifies the management and manipulation of your data. Its JavaScript-based environment ensures flexibility, while its cross-platform compatibility makes it accessible for users on any operating system


Next Article
MongoDB - Regex

K

kumarsar29u2
Improve
Article Tags :
  • MongoDB
  • Databases

Similar Reads

  • When to Use MongoDB?
    MongoDB is a powerful NoSQL database designed for managing unstructured and semi-structured data, offering high flexibility, scalability, and performance. Unlike relational databases such as MySQL, MongoDB employs a schema-less document-oriented data model, making it ideal for modern web application
    6 min read
  • MongoDB vs MySQL
    Both MongoDB and MySQL are popular database management systems (DBMS), but they are built for different purposes and have distinct features. MongoDB is a NoSQL database, designed for handling unstructured data with high scalability, while MySQL is a traditional relational database management system
    6 min read
  • MongoDB - Regex
    The $regex operator in MongoDB is a powerful tool that provides regular expression capabilities for pattern matching within strings in queries. It is particularly useful when the exact field value is unknown which allows for flexible and efficient searches within collections. In this article, We wil
    6 min read
  • MongoDB Views
    In MongoDB, there are two primary types of views such as Standard Views and On-demand Materialized Views. These views offer different ways of managing data without directly modifying the underlying collections. While Standard Views are read-only and provide dynamic data access based on queries. On-d
    7 min read
  • Scaling in MongoDB
    Scaling in MongoDB is a critical process that ensures a database can handle increasing data volumes, user traffic and processing demands. As applications grow, maintaining optimal performance and resource utilization becomes essential. In this article, we will understand Scaling in detail with the n
    10 min read
  • How To Use the MongoDB Shell
    The MongoDB Shell, also known as mongosh is a powerful command-line interface that allows users to interact with MongoDB databases. It provides a REPL (Read-Eval-Print Loop) environment that facilitates database management, data manipulation, and administrative tasks with ease In this comprehensive
    7 min read
  • MongoDB Tutorial
    In today's data-driven world, the ability to efficiently store and manage large amounts of data is crucial. MongoDB, a powerful NoSQL database, has become a go-to choice for developers looking for flexibility, scalability, and performance. Unlike traditional relational databases, MongoDB uses a docu
    10 min read
  • MongoDB Terminology
    MongoDB is a popular NoSQL database designed for handling large volumes of diverse data types. Its flexible schema, high performance and scalability make it ideal for modern applications requiring rapid development and efficient data management. In this article, We will learn about the MongoDB Termi
    7 min read
  • Mongoose Vs MongoDB
    Mongoose is a powerful Object Data Modeling (ODM) library tailored for MongoDB and JavaScript, providing a structured approach to manage data in Node.js applications. By defining schemas that map to MongoDB documents, Mongoose ensures data integrity and simplifies CRUD operations. It offers features
    6 min read
  • How MongoDB works ?
    MongoDB is an open-source document-oriented database. It is used to store a larger amount of data and also allows you to work with that data. MongoDB is not based on the table-like relational database structure but provides an altogether different mechanism for storage and retrieval of data, that's
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences