Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering MongoDB 7.0 - Fourth Edition

You're reading from  Mastering MongoDB 7.0 - Fourth Edition

Product type Book
Published in Feb 2024
Publisher Packt
ISBN-13 9781835883501
Pages 398 pages
Edition 4th Edition
Languages
Concepts
Authors (7):
Marko Aleksendrić Marko Aleksendrić
Profile icon Marko Aleksendrić
Arek Borucki Arek Borucki
Profile icon Arek Borucki
Leandro Domingues Leandro Domingues
Profile icon Leandro Domingues
Malak Abu Hammad Malak Abu Hammad
Profile icon Malak Abu Hammad
Elie Hannouch Elie Hannouch
Profile icon Elie Hannouch
Rajesh Nair Rajesh Nair
Profile icon Rajesh Nair
Rachelle Palmer Rachelle Palmer
Profile icon Rachelle Palmer
View More author details

Table of Contents (20) Chapters

Preface Chapter 1: Introduction to MongoDB Chapter 2: The MongoDB Architecture Chapter 3: Developer Tools Chapter 4: Connecting to MongoDB Chapter 5: CRUD Operations and Basic Queries Chapter 6: Schema Design and Data Modeling Chapter 7: Advanced Querying in MongoDB Chapter 8: Aggregation Chapter 9: Multi-Document ACID Transactions Chapter 10: Index Optimization Chapter 11: MongoDB Atlas: Powering the Future of Developer Data Platforms Chapter 12: Monitoring and Backup in MongoDB Chapter 13: Introduction to Atlas Search Chapter 14: Integrating Applications with MongoDB Chapter 15: Security Chapter 16: Auditing Chapter 17: Encryption Index Other Books You May Enjoy

Why developers love MongoDB

Along with its versatality, and robust features, MongoDB is the preferred choice for several reasons, such as:

  • Flexibility and schema-less: Unlike traditional relational databases, MongoDB allows you to store and retrieve data without strict schemas or predefined structures. This flexibility is particularly useful when data evolves over time or when you are dealing with unstructured or semi-structured data.
  • Scalability and performance: MongoDB is highly scalable and performs exceptionally well, making it suitable for both large-scale applications and personal projects. MongoDB Atlas provides a free-forever tier for side projects.
  • Rich query language: MongoDB offers a powerful query language and indexing capabilities, simplifying common operations such as findOne and updateOne.
  • Developer-friendly data format: Data in MongoDB closely resembles objects in popular programming languages, reducing data mapping complexities and expediting development.
  • Simplicity and quick start: MongoDB's simplicity and hassle-free setup makes it easy to adapt. No complex sales processes or licensing hassles are involved.

What attracts most developers is the simplicity of working with MongoDB on a daily basis, in particular, the seamless experience of creating, updating, and interacting with data. For example, consider a Python developer attempting to insert a document, query that document, and receive a set of results, using the following code:

from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['mydatabase']  # Specify the database name
collection = db['mycollection']  # Specify the collection name
# Create a document to be inserted
document = {
    'name': 'Jane Doe',
    'age': 30,
    'email': 'janedoe@example.com'
}
# Insert the document into the collection
result = collection.insert_one(document)
# Check if the insertion was successful
if result.acknowledged:
    print('Document inserted successfully.')
    print('Inserted document ID:', result.inserted_id)
else:
    print('Failed to insert document.')

Note that the developer creates a dictionary representing the document to be inserted. In this case, it contains the name, age, and email details. The developer doesn't need to create an ID for the document, because MongoDB automatically creates a unique identifier on each document.

To retrieve this document, you can filter the query by using any of the document's field individually, or in combination. Let's see that in action:

from pymongo import MongoClient
# Connect to MongoDB
client = MongoClient('mongodb://localhost:27017/')
db = client['mydatabase']  # Specify the database name
collection = db['mycollection']  # Specify the collection name
# Retrieve documents based on specific conditions
query = {
    'age': {'$gte': 29},  # Retrieve documents where age is greater than or equal to 29
}
documents = collection.find(query)
# Iterate over the retrieved documents
for document in documents:
    print(document)

Pretty simple! The preceding example demonstrates how you can use a MongoDB query operator such as $gte (greater than or equal to) to filter your query. But the real magic happens when the document is returned. When MongoDB returns a document, it will be represented as a Python dictionary. Each field in the document is a key-value pair within the dictionary, similar to the following example:

{
    '_id': ObjectId('60f5c4c4543b5a2c7c4c73a2'),
    'name': 'Jane Doe',
    'age': 30,
    'email': 'janedoe@example.com'
}

MongoDB has a suite of language libraries and drivers that act as a translation layer between the client and server, intercepting each operation and translating it into MongoDB's query language. With this, you can interact with the data using your native programming language in a purely idiomatic way.

Alongside the other offerings of the MongoDB Atlas developer data platform, it truly abstracts away the difficulties of working with a database, and instead allows you to interact with data purely via your code and IDE. This is infinitely preferable while using a separate database shell, database UI, and other database-specific tools. Since MongoDB Atlas offers a completely managed MongoDB database, you can set up and register via a command-line interface.

At its heart, the mission of MongoDB is to be a powerful database for developers, and its features are tuned to the programming language communities and framework integrations, rather than to database administration tools. This will become more apparent in the subsequent chapters, where you'll learn more about MongoDB Atlas, Atlas Vector Search, full-text search, and features such as aggregation—all through the lens of a developer.

By the end of this book, you'll learn how effective these tools can be and make database management simpler!

You have been reading a chapter from
Mastering MongoDB 7.0 - Fourth Edition
Published in: Feb 2024 Publisher: Packt ISBN-13: 9781835883501
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}