[파이썬] `mongoengine` 활용 사례

MongoDB is a popular NoSQL database that offers high scalability and flexibility. To work with MongoDB in Python, mongoengine is a powerful Object-Document Mapper (ODM) that provides a convenient way to interact with the database using Python objects and classes.

In this blog post, we will explore some practical use cases of mongoengine and demonstrate how it simplifies the development process when working with MongoDB.

Installation

Before we dive into the examples, let’s make sure mongoengine is installed. You can install it using pip:

$ pip install mongoengine

Example 1: Creating a Schema

One of the key features of mongoengine is the ability to define schemas for MongoDB documents using Python classes. Let’s create a simple User schema that consists of the name and email fields:

from mongoengine import Document, StringField

class User(Document):
    name = StringField(required=True)
    email = StringField(required=True, unique=True)

With this schema, we can now create, update, and query User documents in MongoDB with ease.

Example 2: Saving Documents

To save a new User document to the database, we can simply create an instance of the User class and call the save() method:

user = User(name="John Doe", email="john@example.com")
user.save()

This will insert the document into the MongoDB collection associated with the User class.

Example 3: Querying Documents

mongoengine provides a rich set of querying capabilities to retrieve documents from MongoDB. Let’s see an example where we retrieve all users with a specific email domain:

users = User.objects(email__endswith="@example.com")
for user in users:
    print(user.name)

In the above example, we use the objects() method to perform a query. The email__endswith parameter specifies the condition for filtering documents based on the email field.

Example 4: Updating Documents

Updating documents in MongoDB is straightforward with mongoengine. Here’s an example where we update a user’s email address:

user = User.objects(name="John Doe").first()
if user:
    user.email = "johndoe@example.com"
    user.save()

Once we retrieve the user document, we can modify any attribute and call the save() method to persist the changes to the database.

Example 5: Deleting Documents

Deleting documents is also easy with mongoengine. Let’s remove a user from the database using their email address:

user = User.objects(email="john@example.com").first()
if user:
    user.delete()

The delete() method removes the document from the MongoDB collection.

Conclusion

In this blog post, we have seen some practical examples of how to use mongoengine to interact with MongoDB in Python. This powerful ODM simplifies the development process by allowing you to work with MongoDB in an object-oriented manner.

Whether you are building a small application or a large-scale system, mongoengine provides a convenient and intuitive way to work with MongoDB, making it a popular choice among Python developers.

Give it a try in your next project and experience the productivity and flexibility it brings when working with MongoDB!