0% found this document useful (0 votes)
9 views

Tuto Mongosh

mongosh is an interactive JavaScript shell for managing MongoDB databases, providing a command-line interface for executing commands and queries. It allows users to perform CRUD operations, manage databases and collections, and automate tasks with real-time feedback. Installation can be done via package managers or manually, and users can connect to local or remote MongoDB instances using specific connection strings.

Uploaded by

qassiyassine1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Tuto Mongosh

mongosh is an interactive JavaScript shell for managing MongoDB databases, providing a command-line interface for executing commands and queries. It allows users to perform CRUD operations, manage databases and collections, and automate tasks with real-time feedback. Installation can be done via package managers or manually, and users can connect to local or remote MongoDB instances using specific connection strings.

Uploaded by

qassiyassine1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

What is mongosh?

mongosh (MongoDB Shell) is an interactive JavaScript shell used to manage MongoDB


databases. It provides a command-line interface (CLI) for developers and database
administrators to interact with MongoDB instances directly from the terminal.

 mongosh is similar to a REPL (Read-Eval-Print Loop), where you can input JavaScript
code, MongoDB commands, and queries, and receive immediate results.
 It’s built specifically for MongoDB, allowing you to manage databases, collections,
and documents, perform CRUD operations, and execute more advanced features like
aggregation.

Usefulness of mongosh

 Database Management: Create, modify, and delete databases and collections.


 Data Queries: Perform CRUD operations (Create, Read, Update, Delete) on
documents.
 Scripting: Automate database operations and write scripts for repetitive tasks.
 Real-Time Feedback: Immediate results when interacting with the database, helping
to debug and test queries interactively.
 Comprehensive Features: Supports complex querying, aggregation, indexing, and
other MongoDB features.

Checking if mongosh is Installed

To check if mongosh is installed on your system, open your terminal (Command Prompt,
PowerShell, or a terminal on macOS/Linux) and run:

bash

mongosh --version

 If mongosh is installed, it will return the installed version number.


 If mongosh is not installed, you will get an error indicating that the command is not
recognized.

How to Install mongosh

1. Install mongosh via Package Manager (Windows/macOS/Linux)


Windows (using choco):

1. If you have Chocolatey installed, run the following command in PowerShell:


bash

choco install mongosh


macOS (using brew):

1. If you have Homebrew installed, run the following command:

bash

brew install mongosh


Linux:

1. On Ubuntu (or similar distributions), you can install mongosh by running:

bash

sudo apt install mongosh


2. Manual Installation

1. Visit the official MongoDB download page.


2. Select your operating system and download the appropriate version.
3. Follow the installation instructions provided for your system.

How to Connect to MongoDB Server Using mongosh

Once mongosh is installed, you can connect to a MongoDB server.

Basic Syntax:
bash

mongosh "mongodb://<host>:<port>"

 <host>: The hostname or IP address where MongoDB is running (e.g., localhost for
local machine).
 <port>: The port MongoDB is listening on (default is 27017).

1. Connecting to Local MongoDB Instance

If MongoDB is running on your local machine:

bash

mongosh "mongodb://localhost:27017"
2. Connecting to Remote MongoDB Instance

To connect to a remote MongoDB server:

bash

mongosh "mongodb://<remote_host>:<port>"
Replace <remote_host> with the IP address or hostname of the server and <port> with the
appropriate port number.

3. Connecting with Authentication (if enabled)

If your MongoDB instance requires authentication:

bash

mongosh "mongodb://<username>:<password>@localhost:27017"
4. Using MongoDB Atlas (Cloud Service)

If you're connecting to a MongoDB Atlas cloud instance:

1. Go to the MongoDB Atlas dashboard.


2. Copy the connection string provided (make sure to replace the placeholder
<password> with your database password).
3. Use it in mongosh:

bash

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

Example of Connecting to MongoDB

For a local MongoDB instance:

bash

mongosh "mongodb://localhost:27017"

If successful, you will be connected to the MongoDB shell, where you can start running
MongoDB commands.

In mongosh, you can manage databases and collections using straightforward


commands:

Create a Database

MongoDB creates a database automatically when you insert the first document into a
collection. However, you can "prepare" a database like this:

javascript
// Use the database (creates it if it doesn't exist)
use mydatabase

This switches to mydatabase, creating it if necessary, but it won't be permanently created


until you add some data.

Create a Collection

To create a collection explicitly:

javascript

// Create a collection explicitly


db.createCollection("mycollection")

Alternatively, MongoDB will create the collection automatically when you first insert a
document:

javascript

db.mycollection.insertOne({ name: "Younes", age: 30 })

Delete a Collection

To delete a collection:

javascript

db.mycollection.drop()

Delete a Database

To delete the entire database:

javascript

db.dropDatabase()

⚠ Warning: This will permanently delete the database, including all its collections and
documents.

Verify the Changes

 List Databases:
javascript

show dbs

 List Collections in the Current Database:

javascript

show collections

CRUD operations

 Insert Data:

javascript

-db.stagiaires.insertOne({cne:12345,nom:'karimi farid'})
- db.stagiaires.insertMany([
{
nom_prenom: 'el amrani yassine',
classe: 'RVA 102',
cne: 10003
},
{
nom_prenom: 'benmoussa hanae',
classe: 'RVA 103',
cne: 10004
},
{
nom_prenom: 'boukili hamza',
classe: 'RVA 104',
cne: 10005
}
])

 Update Data:

javascript

-db.stagiaires.updateMany({cne:10000},{$set:{nom:'sobhi samia'}})
-db.stagiaires.updateOne({cne:10000},{$set:{nom:'sobhi salim'}})

 Delete Data:

javascript

-db.stagiaires.deleteOne({cne:12345})
-db.stagiaires.deleteMany({cne:10000})

 Extract Data:

javascript
-db.stagiaires.find()

-db.stagiaires.find({cne:{$gt:10000}})

Example Workflow:
javascript

// Use or create the database


use mydatabase

// Create a collection
db.createCollection("mycollection")

// Insert a document
db.mycollection.insertOne({ name: "Younes", age: 30 })

// List collections
show collections

// Drop the collection


db.mycollection.drop()

// Drop the entire database


db.dropDatabase()

You might also like