1
Introduction to MongoDB
MongoDB is the world's most popular NoSQL document database. Rather than using traditional relational tables, it stores data in a flexible, JSON-like format. Developers value it for its ease of scaling, strong security features, and reliable performance. With tools such as AI-enhanced search, sharding, and encryption, MongoDB offers a comprehensive platform for handling diverse data needs.
MongoDB 8.0 is the fastest and highest-performing version of MongoDB yet, performing around 32% faster than the previous version of MongoDB (Source: https://www.mongodb.com/company/blog/mongodb-8-0-improving-performance-avoiding-regressions). In addition to performance gains, MongoDB 8.0 delivers various improvements in aggregation, security, sharding, replication, and more, such as the following:
- Sharding enhancements distribute data across shards up to 50 times faster and at up to 50% lower starting cost, lowering the infrastructure cost of getting started by up to $5,000 (USD) per year
- Improved support for a wide range of search and AI applications at higher scale and lower cost, using quantized vectors (compressed representations of full-fidelity vectors) that require up to 96% less memory and are faster to retrieve while preserving accuracy
- Expanded support for MongoDB's Queryable Encryption, a groundbreaking innovation developed by the MongoDB Cryptography Research Group, to also support range queries
This book provides a comprehensive overview of what MongoDB, particularly version 8.0, can offer for your development needs. While we strive to be comprehensive and current, MongoDB is constantly adding new features. For the most detailed and up-to-date information, check out the MongoDB documentation at https://mongodb.com/docs.
In this chapter, we're going to cover the following topics:
- Why MongoDB?
- Who uses MongoDB?
- MongoDB architecture
- What's new in MongoDB 8.0?
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.
Who uses MongoDB?
MongoDB is used by many different industries, and its use cases span all kinds of situations and types of data. Users range from small businesses and start-ups, and even student projects, to some of the largest banks, automakers, and government agencies in the world.
MongoDB offers three different environments for you to create a database and store your data, each of which supports different developer needs:
- MongoDB Atlas: Atlas is MongoDB's cloud database service. It simplifies deploying and managing your databases, removing the user's responsibility to maintain hardware and keep up with software patches. You can provision your database through the Atlas UI and easily scale as necessary. When you use Atlas, you have access to additional features such as embedded full-text search via Atlas Search, vector-based search through Atlas Vector Search, and Atlas Stream Processing, which allows you to process streams of complex data. Atlas is used by those who want to focus on developing data models instead of spending resources provisioning and managing their database.
- MongoDB Enterprise Advanced (EA): MongoDB EA is the commercial edition of MongoDB. It includes additional capabilities such as an in-memory storage engine for high throughput and low latency, advanced security features such as Kerberos access controls, and encryption for data at rest. EA is ideal for organizations that require self-managed deployments on-premises, in private clouds, or in hybrid environments. The EA subscription, through which you get MongoDB EA, includes 24/7/365 support and tools such as MongoDB Ops Manager to help simplify deployment.
- MongoDB Community: MongoDB Community is the free-to-use version of MongoDB. It includes support for basic operations such as queries, indexing, and aggregation. Because it is free and source-available, MongoDB Community is often used by small businesses and...