[파이썬] mongoengine 경로 및 URL 필드 활용

Mongoengine is a Python Object-Document Mapping (ODM) library that provides a high-level API for interacting with MongoDB databases. In this blog post, we will explore how to use the path and URL fields in Mongoengine to efficiently handle file paths and URLs in Python.

Path Field

The Path field in Mongoengine allows you to store file paths or directories in your MongoDB documents. It provides utility methods to handle the paths easily.

To use the Path field, you need to import it from the mongoengine.fields module:

from mongoengine import Document, StringField, PathField

Let’s create a simple example of a Document class that uses a Path field:

class File(Document):
    file_name = StringField(required=True)
    file_path = PathField(path_type="file", required=True)

In the above example, a File document has two fields: file_name as a string field and file_path as a Path field. We used the path_type="file" argument to specify that this field should represent a file path.

With this setup, you can now easily store and retrieve file paths in your MongoDB documents. Mongoengine will also validate the path string, ensuring that it points to a valid file.

Here’s an example of how to create and save a File document:

file = File(file_name="example.txt", file_path="/path/to/example.txt")
file.save()

You can also retrieve the file path from the database and manipulate it:

file = File.objects().first()
file.file_path.get()  # Get the file path
file.file_path.exists()  # Check if the file exists
file.file_path.rename("/new/path/example.txt")  # Rename the file

The Path field in Mongoengine makes it easier to store and work with file paths in MongoDB, providing more flexibility and convenience in managing file data.

URL Field

In addition to file paths, Mongoengine also provides a URL field to store URLs within your MongoDB documents. The URL field ensures that the URLs are valid and provides utility methods to extract information from them.

To use the URL field, you need to import it from the mongoengine.fields module:

from mongoengine import Document, URLField

Let’s create a simple example of a Document class that uses a URL field:

class Website(Document):
    name = StringField(required=True)
    url = URLField(required=True)

In the above example, a Website document has two fields: name as a string field and url as a URL field.

Now, you can easily store and retrieve URLs in your MongoDB documents. Mongoengine will validate the URL string, ensuring that it is a valid URL.

Here’s an example of how to create and save a Website document:

website = Website(name="Example Website", url="https://www.example.com")
website.save()

You can also retrieve the URL from the database and manipulate it:

website = Website.objects().first()
website.url.get()  # Get the URL
website.url.host()  # Get the host name
website.url.path()  # Get the path

The URL field in Mongoengine simplifies the handling and validation of URLs in MongoDB, making it easier to work with web-related data.

Conclusion

Mongoengine’s Path and URL fields provide convenient ways to store and handle file paths and URLs in MongoDB documents. By using these fields, you can ensure the validity of the paths and URLs and leverage the utility methods provided by Mongoengine. This allows for efficient management of file and web-related data in your Python applications.