mongosh

What is mongosh?

mongosh (MongoDB Shell) is the interactive command-line tool used to connect to and work with a MongoDB database.

It’s the place where you write commands to query, insert, update, and manage data.


🔹 Why mongosh is important


  • It’s the primary interface to interact with MongoDB from terminal
  • Helps in debugging, testing queries, and admin tasks
  • Used by developers and DB admins

How mongosh works ?


You connect to your database:


mongosh "mongodb://localhost:27017"


Basic Commands in mongosh


1. Show databases


show dbs

2. Use a database


use myDatabase

3. Insert data


db.users.insertOne({
  name: "Abinav",
  age: 26
})


4. Read data

db.users.find()

5. Update data


db.users.updateOne(
  { name: "Abinav" },
  { $set: { age: 27 } }
)


6. Delete data


db.users.deleteOne({ name: "Abinav" })

🔹 Key Features of mongosh


JavaScript-based

  • You write queries in JavaScript syntax
  • You can even use variables, loops, functions

let age = 25;
db.users.find({ age: { $gt: age } })

You can run JS files:


mongosh script.js