[파이썬] mongoengine 동적 도큐먼트(`DynamicDocument`) 활용

Mongoengine is a Python Object-Document Mapper (ODM) library for MongoDB. It provides a high-level API that simplifies working with MongoDB in Python. One of the powerful features of mongoengine is the ability to use dynamic documents (DynamicDocument), which allows you to define flexible schemas for your MongoDB collections.

What are Dynamic Documents?

In MongoDB, documents within a collection can have different structures. Unlike traditional relational databases where all records in a table have the same schema, MongoDB allows you to store documents with varying fields and structures within the same collection. This flexibility is one of the key advantages of using MongoDB.

With mongoengine’s DynamicDocument, you can take advantage of this flexibility in your Python code. You can define models that are not bound to a specific schema and can have dynamic fields that can be added or modified at runtime.

How to use DynamicDocument in mongoengine

To use DynamicDocument, you need to import it from the mongoengine library:

from mongoengine import DynamicDocument, StringField, DictField

Next, define your dynamic model by subclassing DynamicDocument:

class MyDynamicDocument(DynamicDocument):
    name = StringField(required=True)
    data = DictField()

In the above example, we define a MyDynamicDocument model with a required name field of type StringField and a data field of type DictField. The DictField allows us to store dynamic fields and their values as key-value pairs.

You can then create and save instances of your dynamic document:

doc = MyDynamicDocument(name="Dynamic Document Example", data={"field1": "value1", "field2": "value2"})
doc.field3 = "value3"  # Add a new dynamic field
doc.save()

You can also query and retrieve the dynamic fields:

doc = MyDynamicDocument.objects(name="Dynamic Document Example").first()
print(doc.name)
print(doc.data["field1"])
print(doc.field3)

Benefits of using Dynamic Documents

Using DynamicDocument in mongoengine offers a range of benefits:

  1. Flexibility: Dynamic documents allow you to store and retrieve data with varying structures, which is ideal for scenarios where the schema evolves over time.

  2. Simplicity: You can work with MongoDB’s flexibility without the need to define and manage multiple explicit schemas. This reduces development and maintenance efforts.

  3. Dynamic Fields: With dynamic documents, you can add, modify, or delete fields at runtime. This enables you to adapt to new requirements without schema migrations.

  4. Querying: You can still query and retrieve documents based on the dynamic fields. mongoengine supports querying dynamic fields using the same syntax as for regular fields.

Conclusion

Dynamic documents provided by mongoengine’s DynamicDocument class offer great flexibility when working with MongoDB in Python. By using a dynamic schema, you can adapt to changing requirements and store documents with varying structures within the same collection. Dynamic documents simplify the development process and allow for easier data management.