![]() |
VOOZH | about |
MongoDB Shell (mongosh) is an interactive, JavaScript-based command-line tool used to directly interact with and manage MongoDB databases.
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:
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.
MongoDB Shell comes packed with essential features that enhance the database management experience. Here are some key highlights:
Install MongoDB Shell, launch it from the terminal or command prompt, connect to MongoDB, and start interacting with databases using commands.
1. Install MongoDB Shell:
2. Launch the Shell:
mongosh to start the MongoDB Shell.3. Access Help:
help within the shell to get a list of available commands and their descriptions.To connect to a MongoDB deployment, you need a connection string. The connection string typically includes your MongoDB instance's address and authentication details.
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 tasks.
Some fundamental commands and operations you can perform using MongoDB Shell:
1. Show Databases: To view the list of databases, use the command:
show dbs2. Switch Database: You can switch to a specific database using the use command.
use mydatabase3. Show Collections: To list all collections within the current database, use:
show collections4. 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()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" })