codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🍃

MongoDB

5 / 65 topics
1Introduction to MongoDB2MongoDB Architecture3Installation and Setup4Connecting to MongoDB5MongoDB Shell Basics
Tutorials/MongoDB/MongoDB Shell Basics
🍃MongoDB

MongoDB Shell Basics

Updated 2026-04-20
3 min read

Introduction

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.

Prerequisites

Before starting this tutorial, ensure that you have:

  • Installed MongoDB on your machine.
  • Started the MongoDB server.
  • Basic understanding of databases and collections.

Connecting to MongoDB

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

Basic Commands

Displaying Databases

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.

Switching Databases

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

Displaying Collections

To list all collections in the current database, use the show collections command:

show collections

Managing Collections

Creating 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.

Inserting Documents

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 }
])

Querying Documents

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.

Updating Documents

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.

Deleting Documents

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.

Advanced Queries

Sorting Documents

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 })

Limiting Results

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.

Aggregation Framework

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.

Best Practices

  • Use Descriptive Collection Names: Choose meaningful names that reflect the data stored in the collection.
  • Indexing: Create indexes on fields that are frequently queried to improve performance.
  • Validation Rules: Use validation rules to ensure data integrity and consistency.
  • Security: Secure your MongoDB instance by enabling authentication, using strong passwords, and limiting access.

Conclusion

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.


PreviousConnecting to MongoDBNext Data Modeling Concepts

Recommended Gear

Connecting to MongoDBData Modeling Concepts