📜  mongoengine - Python (1)

📅  最后修改于: 2023-12-03 15:32:57.531000             🧑  作者: Mango

Mongoengine - Python

Introduction

Mongoengine is a Python Object-Document Mapper (ODM) for working with MongoDB. It provides a clean and pythonic API for querying and performing CRUD operation on MongoDB collections.

Installation

You can install mongoengine using pip:

pip install mongoengine
Usage
Connecting to MongoDB
from mongoengine import connect

connect('my_database', host='localhost', port=27017)
Creating a Document
from mongoengine import Document, StringField

class User(Document):
    name = StringField()
    email = StringField()
Saving a Document
user = User(name='John Doe', email='john.doe@example.com')
user.save()
Updating a Document
user = User.objects(name='John Doe').first()
user.email = 'john.doe.update@example.com'
user.save()
Querying Documents
users = User.objects(email__contains='example.com')
for user in users:
    print(user.name, user.email)
Conclusion

Mongoengine is a powerful tool for working with MongoDB in Python. It provides a clean and pythonic API for querying and performing CRUD operation on MongoDB collections.