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