MongoDB Shell (mongosh) Introduction

 

MongoDB Shell (mongosh) Introduction

📌 Introduction

MongoDB is one of the most popular NoSQL databases used for managing large-scale data efficiently. Whether you're a developer or a student working with MongoDB, you'll often need a way to interact directly with your database. That's where MongoDB Shell (mongosh) comes into play.

 

In this blog, we’ll explore:

 

·         What is mongosh

·         Key features

·         Basic usage with examples

·         How to install and run it

·         Differences from the older mongo shell

 

 

     What is mongosh?

Mongosh (MongoDB Shell) is an interactive command-line interface (CLI) that allows you to:

 

·         Connect to MongoDB databases

·         Run queries and commands

·         Manage collections and documents

·         Perform administrative tasks

 

It’s a JavaScript-based shell and is the modern replacement for the legacy mongo shell.

 

 

🌟 Key Features of mongosh

·         Modern JavaScript syntax support (like ES6+)

·         Easy integration with MongoDB Atlas

·         Works well with MongoDB Compass

·         Supports auto-completion, syntax highlighting, and rich output

·         Allows usage of Node.js modules


⚙️ Installing mongosh

Using npm (for Node.js users):

npm install -g mongosh

 

Using MongoDB Installer:

·         Visit https://www.mongodb.com/try/download/shell

·         Choose your OS and follow the instructions After installation, run:

mongosh

 

🔗 Connecting to a MongoDB Server

▶️  For  Localhost:

mongosh

 

▶️ For Remote MongoDB (e.g., Atlas):

mongosh "mongodb+srv://<username>:<password>@cluster0.mongodb.net/myFirstDatabase"

 

Basic Commands in mongosh

Here are some commonly used commands:

 

📁 Show databases

show dbs




📌 Switch/Create database

use studentDB


📂 Show collections

show collections

Insert a document

db.students.insertOne({ name: "Aarya", age: 21 })

 

 

Q Find documents

db.students.find()




 

🔄 Update a document

db.students.updateOne({ name: "Aarya" }, { $set: { age: 22 } })




 

Delete a document


db.students.deleteOne({ name: "Aarya" })




 

 

 

🔄 mongosh vs mongo

 

Feature

mongo (Old)

mongosh (New)

JavaScript Support

Limited (ES5)

Modern (ES6+)

Auto-completion

No

Yes

Node.js Integration

No

Yes

Active Development

Deprecated

Actively maintained

 

📚 Conclusion

MongoDB Shell (mongosh) is a powerful and user-friendly tool for working with MongoDB databases. Whether you’re inserting documents or performing admin tasks, mongosh makes it easier with its modern features and intuitive interface.

 

If you're just getting started with MongoDB, learning mongosh is the perfect first step toward mastering NoSQL databases.

 

 

Next Steps

·         Practice basic CRUD operations

·         Explore integration with MongoDB Compass

·         Try connecting with MongoDB Atlas

 

💬 “If you can speak JavaScript, you can control your database – that’s the power of mongosh!”


Rohit Suryawanshi

University: Sri Balaji University, Pune

School: School of Computer Studies

Course: BCA (Bachelor of Computer Applications)

Interests:MongoDB Shell (mongosh) Introduction

📸 Instagram 🔗 LinkedIn 🌐 Official Website

Comments

Post a Comment

Popular posts from this blog

Query Operator's

Creating Documents in MongoDB(Insert)