The MongoDB Shell, also known as mongo, is a command-line interface for interacting with MongoDB databases. It allows you to execute commands and scripts to manage your data, perform queries, and automate tasks. This tutorial will cover the basics of using the MongoDB Shell, including connecting to a database, executing basic commands, and managing collections.
Before starting this tutorial, ensure that you have:
To start the MongoDB Shell, open your terminal or command prompt and type:
mongo
This will connect you to the default test database. If you want to connect to a specific database, use the following command:
mongo <database_name>
For example, to connect to a database named myDatabase, run:
mongo myDatabase
To list all available databases, use the show dbs command:
show dbs
This will display all databases that have at least one collection with data.
To switch to a different database, use the use command followed by the database name:
use <database_name>
For example, to switch to the myDatabase database, run:
use myDatabase
To list all collections in the current database, use the show collections command:
show collections
You can create a new collection by inserting data into it. MongoDB will automatically create the collection if it doesn't exist.
db.myCollection.insertOne({ name: "John Doe", age: 30 })
This command inserts one document into myCollection and creates the collection if it doesn't already exist.
To insert a single document, use the insertOne method:
db.myCollection.insertOne({ name: "Jane Smith", age: 25 })
To insert multiple documents, use the insertMany method:
db.myCollection.insertMany([
{ name: "Alice Johnson", age: 35 },
{ name: "Bob Brown", age: 40 }
])
To find all documents in a collection, use the find method:
db.myCollection.find()
This will return all documents in myCollection.
To query specific documents, pass a filter object to the find method:
db.myCollection.find({ age: 30 })
This will return all documents where the age field is 30.
To update a single document, use the updateOne method:
db.myCollection.updateOne(
{ name: "John Doe" }, // Filter
{ $set: { age: 31 } } // Update operation
)
This command updates the age field of the document where name is "John Doe".
To update multiple documents, use the updateMany method:
db.myCollection.updateMany(
{ age: { $lt: 30 } }, // Filter
{ $inc: { age: 1 } } // Update operation
)
This command increments the age field by 1 for all documents where age is less than 30.
To delete a single document, use the deleteOne method:
db.myCollection.deleteOne({ name: "Jane Smith" })
To delete multiple documents, use the deleteMany method:
db.myCollection.deleteMany({ age: { $gt: 40 } })
This command deletes all documents where age is greater than 40.
To sort documents, use the sort method. For example, to sort documents by age in ascending order:
db.myCollection.find().sort({ age: 1 })
To sort in descending order, use -1:
db.myCollection.find().sort({ age: -1 })
To limit the number of results returned by a query, use the limit method:
db.myCollection.find().limit(5)
This command returns only the first 5 documents.
MongoDB's aggregation framework allows you to process data records and return computed results. Here's an example that groups documents by age and counts them:
db.myCollection.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } }
])
This command returns a document for each unique age value, along with the number of occurrences.
The MongoDB Shell is a powerful tool for interacting with MongoDB databases. By mastering the basics covered in this tutorial, you'll be able to perform essential tasks such as connecting to databases, managing collections, and executing queries. As you become more comfortable with the shell, explore advanced features like aggregation pipelines and indexing to optimize your database operations.
For further learning, refer to the MongoDB official documentation for comprehensive guides and best practices.