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