In this section, we will cover the fundamental concepts of MongoDB, including its architecture, basic operations, and how to interact with a MongoDB database. By the end of this section, you should have a solid understanding of how MongoDB works and be able to perform basic operations.

Key Concepts

  1. Document-Oriented Database: MongoDB is a NoSQL database that stores data in flexible, JSON-like documents.
  2. Collections: A collection is a group of MongoDB documents, similar to a table in relational databases.
  3. Documents: Documents are the basic unit of data in MongoDB, similar to rows in relational databases. They are stored in BSON format (Binary JSON).
  4. Fields: Fields are key-value pairs in documents, similar to columns in relational databases.

MongoDB Architecture

  • Database: A container for collections.
  • Collection: A container for documents.
  • Document: A set of key-value pairs.

Example Structure

Database: myDatabase
  Collection: users
    Document: { "_id": 1, "name": "John Doe", "age": 30, "email": "[email protected]" }
    Document: { "_id": 2, "name": "Jane Smith", "age": 25, "email": "[email protected]" }

Basic Operations

Connecting to MongoDB

To interact with MongoDB, you need to connect to a MongoDB server. This can be done using the MongoDB shell or a MongoDB driver for your preferred programming language.

Using MongoDB Shell

mongo

This command connects to the default MongoDB server running on localhost at port 27017.

Creating a Database

In MongoDB, you don't need to explicitly create a database. Simply switch to a database, and it will be created if it doesn't exist.

use myDatabase

Creating a Collection

Collections are created implicitly when you insert a document into them. However, you can also create a collection explicitly.

db.createCollection("users")

Inserting Documents

You can insert documents into a collection using the insertOne or insertMany methods.

db.users.insertOne({ "name": "John Doe", "age": 30, "email": "[email protected]" })
db.users.insertMany([
  { "name": "Jane Smith", "age": 25, "email": "[email protected]" },
  { "name": "Alice Johnson", "age": 28, "email": "[email protected]" }
])

Querying Documents

You can query documents using the find method.

db.users.find({ "age": { "$gt": 25 } })

This query returns all documents where the age is greater than 25.

Updating Documents

You can update documents using the updateOne or updateMany methods.

db.users.updateOne({ "name": "John Doe" }, { "$set": { "age": 31 } })
db.users.updateMany({ "age": { "$lt": 30 } }, { "$set": { "status": "active" } })

Deleting Documents

You can delete documents using the deleteOne or deleteMany methods.

db.users.deleteOne({ "name": "John Doe" })
db.users.deleteMany({ "age": { "$lt": 30 } })

Practical Exercises

Exercise 1: Insert and Query Documents

  1. Insert a document into the products collection with the following data:

    { "name": "Laptop", "price": 1200, "category": "Electronics" }
    
  2. Query the document to find all products in the "Electronics" category.

Solution

use myDatabase
db.products.insertOne({ "name": "Laptop", "price": 1200, "category": "Electronics" })
db.products.find({ "category": "Electronics" })

Exercise 2: Update and Delete Documents

  1. Update the price of the "Laptop" to 1100.
  2. Delete all documents in the products collection where the price is less than 1000.

Solution

db.products.updateOne({ "name": "Laptop" }, { "$set": { "price": 1100 } })
db.products.deleteMany({ "price": { "$lt": 1000 } })

Common Mistakes and Tips

  • Mistake: Forgetting to use the correct data types in queries.

    • Tip: Ensure that the data types in your queries match the data types in your documents.
  • Mistake: Not using indexes for frequently queried fields.

    • Tip: Create indexes on fields that are frequently queried to improve performance.

Conclusion

In this section, we covered the basics of MongoDB, including its architecture, basic operations, and how to interact with a MongoDB database. You should now be able to create databases and collections, insert, query, update, and delete documents. In the next module, we will dive deeper into CRUD operations and explore more advanced querying techniques.

© Copyright 2024. All rights reserved