Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
The Official MongoDB Guide

You're reading from   The Official MongoDB Guide Resilience, scalability, security and performance

Arrow left icon
Product type Paperback
Published in Sep 2025
Publisher Packt
ISBN-13 9781837021970
Length 368 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (7):
Arrow left icon
Rachelle Palmer Rachelle Palmer
Author Profile Icon Rachelle Palmer
Rachelle Palmer
Jeffrey Allen Jeffrey Allen
Author Profile Icon Jeffrey Allen
Jeffrey Allen
Parker Faucher Parker Faucher
Author Profile Icon Parker Faucher
Parker Faucher
Alison Huh Alison Huh
Author Profile Icon Alison Huh
Alison Huh
Lander Kerbey Lander Kerbey
Author Profile Icon Lander Kerbey
Lander Kerbey
Maya Raman Maya Raman
Author Profile Icon Maya Raman
Maya Raman
Lauren Tran Lauren Tran
Author Profile Icon Lauren Tran
Lauren Tran
+3 more Show less
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Introduction to MongoDB 2. MongoDB Architecture FREE CHAPTER 3. Developer Tools 4. Data Modeling and Index Optimization 5. Queries 6. Database Operations 7. Security 8. MongoDB Atlas 9. Atlas Search 10. Other Books You May Enjoy
11. Index

Why MongoDB?

MongoDB is the preferred developer data platform for several reasons:

  • Flexibility: MongoDB lets you work with data without locking you into rigid schemas. This makes it easier to adapt your application as your data changes, or when handling formats that benefit from a flexible data store, such as unstructured or semi-structured content.
  • Scalability and performance: MongoDB is highly scalable and performant, allowing it to support both large-scale applications and smaller individual projects.
  • Security: MongoDB offers various security methods from user authentication and authorization to data and network encryption, including 8.0 support for OIDC authentication and authorization, and range queries in Queryable Encryption.
  • Query language: MongoDB offers a powerful query language that you can use to access your data, simplifying common operations such as findOne and updateOne. It also offers indexing capabilities for increased query efficiency.
  • Developer-friendly data format: MongoDB stores data in a document format that resembles the structure of objects in many widely used programming languages, which helps simplify data handling and speeds up the development process.
  • Quick start: MongoDB’s simplicity and user-friendly setup make it easy to start using.

Plainly, MongoDB is simple to use. You can interact with your deployment in various ways, such as through programming language drivers, methods in the MongoDB Shell, and database commands. MongoDB provides a simple and streamlined interface for creating, updating, and interacting with data. For example, consider a Python developer attempting to insert a document by using the Python driver:

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': 'Chinazom',
   'email': 'chinazom@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.')

Easy! You don’t need to create an ID for the document, because MongoDB automatically creates one for you. In this case, all the developer needs to define are the document’s name, age, and email details.

Now, suppose the developer wants to retrieve this document by using a query. You can query for equality (for example, searching for documents in which the name is Chinazom). You can also query for inequality. In the following example, we are constructing a query to look for documents whose age is less than or equal to 25.

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': {'$lte': 25}, # Retrieve documents where age is less than or equal to 25
}
documents = collection.find(query)
# Iterate over the retrieved documents
for document in documents:
    print(document)

This example shows how you can use a MongoDB query operator such as $lte to filter a query. MongoDB returns a document that is represented as a Python dictionary, where each field is a key-value pair in the dictionary. See the following example:

{
  '_id': ObjectId('60f5c4c4543b5a2c7c4c73a2'),
  'name': 'Chinazom',
  'age': 24,
  'email': 'chinazom@example.com'
}

As you can see, the _id field has been inserted by MongoDB and is represented as an ObjectId data type. The _id field is a unique and fast-to-generate identifier for each document. It is used as a document’s primary identifier.

MongoDB has a suite of drivers in various programming languages that act as a translation layer between the client and server. By using these drivers, you can interact with the data with your native programming language. You can also interact with your data by using the MongoDB Shell, database commands, and other tools offered by MongoDB.

The mission of MongoDB is to be a powerful database for developers, and its features are developed with programming language communities and framework integrations in mind. This will become more apparent in subsequent chapters, where you’ll learn about CRUD operations, sharding, replication, MongoDB Atlas, and more, all through the lens of a developer.

lock icon The rest of the chapter is locked
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
The Official MongoDB Guide
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 $19.99/month. Cancel anytime
Modal Close icon
Modal Close icon