# How to use MongoDB Shell

how to use **MongoDB Shell (mongosh)** at the command terminal:

#### **1. Start MongoDB Shell**

If MongoDB is running, open your terminal or command prompt and type:

```sh
mongosh
```

This connects to the **local MongoDB instance** (`mongodb://localhost:27017` by default).

***

#### **2. Check the MongoDB Version**

After launching `mongosh`, you can verify the version:

```sh
db.version()
```

***

#### **3. Show Available Databases**

To list all databases:

```sh
show dbs
```

***

#### **4. Create or Switch to a Database**

To create or switch to a database (`testDB` in this case):

```sh
use testDB
```

***

#### **5. Create a Collection and Insert Documents**

Collections in MongoDB are equivalent to tables in SQL.

**Insert a Single Document**

```sh
db.users.insertOne({ name: "Alice", age: 28, city: "New York" })
```

**Insert Multiple Documents**

```sh
sdb.users.insertMan([
  { name: "Bob", age: 34, city: "Chicago" },
  { name: "Charlie", age: 29, city: "San Francisco" }
])
```

***

#### **6. Retrieve Data**

**Find All Documents**

```sh
sdb.users.find()
```

**Find a Specific Document**

```sh
db.users.findOne({ name: "Alice" })
```

**Find Documents with Filtering**

```sh
db.users.find({ age: { $gt: 30 } })  // Finds users older than 30
```

***

#### **7. Update Documents**

**Update a Single Document**

```sh
db.users.updateOne({ name: "Alice" }, { $set: { age: 29 } })
```

**Update Multiple Documents**

```sh
db.users.updateMany({ city: "Chicago" }, { $set: { city: "New York" } })
```

***

#### **8. Delete Documents**

**Delete One Document**

```sh
db.users.deleteOne({ name: "Charlie" })
```

**Delete Multiple Documents**

```sh
db.users.deleteMany({ age: { $lt: 30 } })
```

***

#### **9. Drop a Collection**

To delete the `users` collection:

```sh
db.users.drop()
```

***

#### **10. Exit MongoDB Shell**

To exit `mongosh`:

```sh
exit
```

***

#### **Final Output Example**

After executing `db.users.find().pretty()`, you might see:

```json
[
  {
    "_id": ObjectId("65ab45f12345"),
    "name": "Alice",
    "age": 29,
    "city": "New York"
  },
  {
    "_id": ObjectId("65ab45f12346"),
    "name": "Bob",
    "age": 34,
    "city": "New York"
  }
]
```

***

This example covers the **basic CRUD (Create, Read, Update, Delete)** operations in MongoDB Shell (`mongosh`). 🚀


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reactjs.koida.tech/nosql-database/how-to-use-mongodb-shell.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
