flask_mongoengine/MongoEngine Defining documents/Model

本文档介绍了MongoEngine在定义文档模型时的详细过程,包括基础文档视图的创建,动态文档schema,嵌入式文档,文档集合设置,设限集合,索引,排序以及文档继承等关键概念。强调了在定义文档视图时选择正确的field类型的重要性,并提供了如何指定默认排序和分片键的示例。
摘要由CSDN通过智能技术生成

关于非关系型数据库与关系型数据库在数据库(文档)或者表(集合)结构上的定义——the principal difference is that no schema is enforced at a database level(主要区别在于非关系型数据库在数据库级别没有被强制使用视图).

定义一个基础文档视图

To define a schema for a document, create a class that inherits from Document. Fields are specified by adding field objects as class attributes to the document class(创建一个继承了Document的模型类即可,然后为期添加字段属性)

假设我们在一个项目中实例化了MongoEngine:

db = MongoEngine()

然后我们在一个模型文件中去定义文档视图:

注意:field类型在定义文档视图的时候要正确选择

from app import db

class User(db.Document):
    username = db.StringField(max_length=255, required=True)
    email = db.EmailField(max_length=255)
    password_hash = db.StringField(required=True)
    create_time = db.DateTimeField(default=datetime.datetime.now, required=True)
    last_login = db.DateTimeField(default=datetime.datetime.now, required=True)
    is_email_confirmed = db.BooleanField(default=False)

Dynamic document schemas(动态文档视图)

DynamicDocument documents work in the same way as Document but any data / attributes set to them will also be saved(动态文档基本工作原理和和普通文档一样,只是在文档定义范围外被设置的属性也会保存在数据库中)
class Page(DynamicDocument):
    title = StringField(max_length=200, required=True)

这里写图片描述

注意:

There is one caveat on Dynamic Documents: fields cannot start with _

Embedded documents(动态文档类型)

MongoDB has the ability to embed documents within other documents. Schemata may be defined for these embedded documents, just as they may be for regular documents. To create an embedded document, just define a document as usual, but inherit from EmbeddedDocument rather than Document(内嵌文档定义的时候继承的是EmbeddedDocument 而不是Document)
class Comment(EmbeddedDocument):
    content = StringField()

class Page(Document):
    comments = ListField(EmbeddedDocumentField(Comment))
    title = StringField(max_length=200, required=True)

这里写图片描述

Skipping Document validation on save

page.save(validate=False) # won't

Document collections(文档集合设置)

Document classes that inherit directly from Document will have their own collection in the database. The name of the collection is by default the name of the class, converted to lowercase (so in the example above, the collection would be called page). If you need to change the name of the collection (e.g. to use MongoEngine with an existing database), then create a class dictionary attribute called meta on your document, and set collection to the name of the collection that you want your document class to use:
默认创建的模型类名就是集合名称的小写形式,如果想要改变集合的名称你就需要在类中创建一个类字典属性meta,然后设置你的集合名称,如下:
class Page(Document):
    title = StringField(max_length=200, required=True)
    meta = {'collection': 'cmsPage'}

Capped collections(设限的集合)

A Document may use a Capped Collection by specifying max_documents and max_size in the meta dictionary. max_documents is the maximum number of documents that is allowed to be stored in the collection, and max_size is the maximum size of the collection in bytes. max_size is rounded up to the next multiple of 256 by MongoDB internally and mongoengine before. Use also a multiple of 256 to avoid confusions. If max_size is not specified and max_documents is, max_size defaults to 10485760 bytes (10MB). The following example shows a Log document that will be limited to 1000 entries and 2MB of disk space,
通过在模型类的meta属性中定义max_documents以及max_size我们可以限制集合存储的记录数以及字节数,以下例子就是限制集合的记录数为1000,字节数2MB:
class Log(Document):
    ip_address = StringField()
    meta = {'max_documents': 1000, 'max_size': 2000000}

Indexes(索引)

You can specify indexes on collections to make querying faster. This is done by creating a list of index specifications called indexes in the meta dictionary, where an index specification may either be a single field name, a tuple containing multiple field names, or a dictionary containing a full index definition.
我们可以在模型类的meta字典属性中定义indexes来创建索引使得查询更快
class Page(Document):
    category = IntField()
    title = StringField()
    rating = StringField()
    created = DateTimeField()
    meta = {
        'indexes': [
            'title',
            '$title',  # text index
            '#title',  # hashed index
            ('title', '-rating'),
            ('category', '_cls'),
            {
                'fields': ['created'],
                'expireAfterSeconds': 3600
            }
        ]
    }

Ordering(排序)

A default ordering can be specified for your QuerySet using the ordering attribute of meta. Ordering will be applied when the QuerySet is created, and can be overridden by subsequent calls to order_by().

class BlogPost(Document):
    title = StringField()
    published_date = DateTimeField()

    meta = {
        'ordering': ['-published_date']
    }

Shard keys (分片关键字)

If your collection is sharded, then you need to specify the shard key as a tuple, using the shard_key attribute of meta. This ensures that the shard key is sent with the query when calling the save() or update() method on an existing Document instance:

class LogEntry(Document):
    machine = StringField()
    app = StringField()
    timestamp = DateTimeField()
    data = StringField()

    meta = {
        'shard_key': ('machine', 'timestamp',)
    }

Document inheritance(文档继承)

To create a specialised type of a Document you have defined, you may subclass it and add any extra fields or methods you may need. As this is new class is not a direct subclass of Document, it will not be stored in its own collection; it will use the same collection as its superclass uses. This allows for more convenient and efficient retrieval of related documents – all you need do is set allow_inheritance to True in the meta data for a document.:
# Stored in a collection named 'page'
class Page(Document):
    title = StringField(max_length=200, required=True)

    meta = {'allow_inheritance': True}

# Also stored in the collection named 'page'
class DatedPage(Page):
    date = DateTimeField()
mongoengineflask_mongoengine 都是 MongoDB 的 Python ORM 框架,它们都提供了一些方便的功能来操作 MongoDB 数据库。它们之间的主要区别在于: 1. 适用范围:mongoengine 是一个独立的 Python ORM 框架,可以在任何 Python 项目中使用。而 flask_mongoengine 是基于 Flask 的扩展,专门为 Flask 应用程序提供 MongoDB 的 ORM 功能。 2. 集成方式:mongoengine 可以与任何 Python Web 框架进行集成,例如 Flask、Django 等。而 flask_mongoengineFlask 的扩展,可以直接在 Flask 应用程序中使用,无需额外的配置。 3. 使用方式:mongoengine 的使用方式与 Django 的 ORM 类似,可以通过定义模型类来操作 MongoDB 数据库。而 flask_mongoengine 的使用方式与 Flask 的其他扩展类似,需要先创建一个 Flask 应用程序实例,然后通过初始化扩展来启用 MongoDB 的 ORM 功能。 4. 功能支持:mongoengine 提供了比较全面的 MongoDB ORM 功能,包括数据模型定义、数据查询、数据更新、数据删除、索引定义等。而 flask_mongoengine 则是在 mongoengine 的基础上,为 Flask 应用程序提供了一些方便的功能,例如初始化数据库连接、请求上下文中自动关闭数据库连接、集成 Flask-Admin 管理界面等。 综上所述,mongoengineflask_mongoengine 都是优秀的 MongoDB 的 Python ORM 框架,选择哪一个取决于你的具体需求和项目架构。如果你正在使用 Flask 架构,并且需要 MongoDB ORM 功能,那么 flask_mongoengine 是一个不错的选择。如果你需要一个独立的 MongoDB ORM 框架,并且想要在多个 Python Web 框架中使用,那么 mongoengine 是一个更好的选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值