Mongoengine 简明教程

MongoEngine - Extensions

MongoEngine 与以下库完美集成:

MongoEngine integrates beautifully with the following libraries −

marshmallow_mongoengine

marshmallow 是一款与 ORM/ODM/框架无关的序列化/反序列化库,用于在复杂数据类型(例如对象)和原生 Python 数据类型之间进行转换。使用 MongoEngine 的此扩展,我们可以轻松地执行序列化/反序列化操作。

marshmallow is an ORM/ODM/framework independent serialization/deserialization library for converting complex datatypes, such as objects, to and from native Python datatypes. Using this extension of MongoEngine, we can easily perform serialize/deserialize operations.

首先,像往常一样创建 Document 类,如下所示:

First, create a Document class as usual as follows −

import mongoengine as me
class Book(me.Document):
title = me.StringField()

然后使用以下代码生成 marshmallow 模式:

Then generate marshmallow schema with the code below −

from marshmallow_mongoengine import ModelSchema
class BookSchema(ModelSchema):
   class Meta:
      model = Book

b_s = BookSchema()

使用如下代码保存文档:

Save a document using the code:

book = Book(title='MongoEngine Book').save()

并使用 dump(0 执行序列化/反序列化,并使用以下代码加载():

And perform serialization/deserialization using dump(0 and load() using the code below −

data = b_s.dump(book).data
b_s.load(data).data

Flask-MongoEngine

这是一个为 MongoEngine 提供集成的 Flask 扩展。该库可以轻松处理应用程序的 MongoDB 数据库连接管理。您还可以将 WTForms 作为模型的模型表单使用。

This is a Flask extension that provides integration with MongoEngine. Connection management of MongoDB database for your app is handled easily by this library. You can also use WTForms as model forms for your models.

安装 flask-mongoengine 包后,使用以下设置初始化 Flask 应用程序:

After installation of flask-mongoengine package, initialize flask app with the following settings −

from flask import Flask
from flask_mongoengine import MongoEngine

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {
   'db': 'mydata',
   'host': 'localhost',
   'port':27017
}
db = MongoEngine(app)

然后使用以下代码定义 Document 子类:

Then define a Document sub class using the below code −

class book(me.Document):
name=me.StringField(required=True)

声明上述类的对象,并在访问特定路由时调用 save() 方法。

Declare an object of above class and call save() method when a particular route is visited.

@app.route('/')
def index():
b1=book(name='Introduction to MongoEngine')
b1.save()
return 'success'

extras-mongoengine

此扩展包含其他字段类型和任何其他奥术。

This extension contains additional Field Types and any other wizardry.

Eve-MongoEngine

Eve 是一个为人类设计的开源 Python REST API 框架。它允许轻松构建和部署高度可自定义且功能齐全的 RESTful Web 服务。

Eve is an open source Python REST API framework designed for human beings. It allows to effortlessly build and deploy highly customizable, fully featured RESTful Web Services.

Eve 由 Flask 和 Cerberus 提供支持,并提供对 MongoDB 数据存储的本机支持。Eve-MongoEngine 为 Eve 提供了 MongoEngine 集成。

Eve is powered by Flask and Cerberus and it offers native support for MongoDB data stores. Eve-MongoEngine provides MongoEngine integration with Eve.

使用以下代码安装和导入该扩展:

Install and import the extension using the code below −

import mongoengine
from eve import Eve
from eve_mongoengine import EveMongoengine

配置设置并初始化 Eve 实例。

Configure the settings and initialize the Eve instance.

my_settings = {
'MONGO_HOST': 'localhost',
'MONGO_PORT': 27017,
'MONGO_DBNAME': 'eve_db'
app = Eve(settings=my_settings)
# init extension
ext = EveMongoengine(app)

按所示定义一个文档类:

Define a Document class as shown below −

class Person(mongoengine.Document):
name = mongoengine.StringField()
age = mongoengine.IntField()

添加模型并运行应用程序,最后使用以下代码:

Add the model and run the application, finally using the below code −

ext.add_model(Person)
app.run()

Django-MongoEngine

此扩展的目的是将 MongoEngine 与 Django API 集成在一起,这是一个非常流行的 Python Web 开发框架。此项目仍在开发中。

This extension aims to integrate MongoEngine with Django API, a very popular Python web development framework. This project is still under development.