MongoEngine: CRUD Operations – Tutorial & Examples

Updated: February 10, 2024 By: Guest Contributor Post a comment

Introduction

MongoEngine is a popular Document-Object Mapper (DOM) for working with MongoDB in Python. It provides an intuitive way to interact with MongoDB collections as Python objects. In this tutorial, we’ll cover the basics of CRUD operations using MongoEngine, offering examples from basic setups to more advanced scenarios. Whether you’re just starting with MongoDB or looking to refine your database operations, this guide is for you.

Getting Started

First, ensure you have MongoDB installed and running on your machine. Next, install MongoEngine by running pip install mongoengine in your terminal. Once installed, connect to your MongoDB instance:

from mongoengine import connect
connect('your_db_name')

This will connect to the local MongoDB instance. Adjust the connection string accordingly if your database resides elsewhere.

Defining Documents

In MongoEngine, every collection is represented as a ‘Document’ class. Here’s a simple example where we define a Person document:

from mongoengine import *

class Person(Document):
    name = StringField(required=True)
    age = IntField()
    email = EmailField()

This class maps to a MongoDB collection with fields for name, age, and email, each with their respective types.

Create Operation

To add a new record, instantiate the Document class with some data and call the save method:

new_person = Person(name='John Doe', age=30, email='[email protected]')
new_person.save()

This creates a new document in the Person collection.

Read Operation

To retrieve documents, use the objects attribute. Here’s how to find all persons:

for person in Person.objects:
    print(person.name, person.age)

And to find a single person by name:

john = Person.objects(name='John Doe').first()
if john:
    print('Found:', john.name)

You can also use filters, like finding all persons over 25:

adults = Person.objects(age__gt=25)
for adult in adults:
    print(adult.name)

Update Operation

To update a document, find the document then update its fields and call save:

john = Person.objects(name='John Doe').first()
if john:
    john.age += 1
    john.save()

To update multiple documents at once, use the update method:

Person.objects(age__lt=18).update(set__age=18)

This will set all persons under 18 to be 18 years old.

Delete Operation

To delete a document, find it and call the delete method:

john = Person.objects(name='John Doe').first()
if john:
    john.delete()

To delete multiple documents, apply a filter then call delete:

Person.objects(age__gt=100).delete()

This deletes all persons older than 100.

Advanced Scenarios

Embedded Documents

Embed another document inside a document to create complex hierarchical structures. For example:

class Address(EmbeddedDocument):
    city = StringField()
    country = StringField()

class Person(Document):
    name = StringField(required=True)
    age = IntField()
    address = EmbeddedDocumentField(Address)

Then, create a person with an address:

johns_address = Address(city='New York', country='USA')
john = Person(name='John Doe', age=30, address=johns_address)
john.save()

You can now query based on embedded document fields as well:

ny_residents = Person.objects(address__city='New York')
for resident in ny_residents:
    print(resident.name)

Dynamic Documents

MongoEngine supports dynamic document schemas. This is helpful when your data schema isn’t fixed. Here’s how:

class DynamicPerson(DynamicDocument):
    name = StringField(required=True)

This allows you to add any number of additional fields on the fly:

john = DynamicPerson(name='John Doe')
john.occupation = 'Software Developer'
john.save()

Conclusion

MongoEngine makes working with MongoDB collections in Python straightforward and intuitive. Through the CRUD operations covered in this tutorial, you’ve learned how to manage your documents efficiently. Remember, practice is key to mastering these concepts, so keep experimenting with different data models and queries.