document¶
This module extend the power of mongoengine.Document
.
-
class
mongoengine_mate.document.
ExtendedDocument
(*args, **values)[source]¶ Provide mongoengine.Document more utility methods.
中文文档
为默认的
mongoengine.Document
提供了更多的便捷方法。-
to_dict
(include_none=True)[source]¶ Convert to dict.
Parameters: include_none (bool) – if False, None value field will be removed. Return type: Dict[str, Any]
-
to_OrderedDict
(include_none=True)[source]¶ Convert to OrderedDict.
Parameters: include_none – bool, if False, None value field will be removed.
-
absorb
(other)[source]¶ For attributes of others that value is not None, assign it to self.
Return type: dict 中文文档
将另一个文档中的数据更新到本条文档。当且仅当数据值不为None时。
-
revise
(data)[source]¶ Revise attributes value with dictionary data.
Return type: dict 中文文档
将一个字典中的数据更新到本条文档。当且仅当数据值不为None时。
-
classmethod
collection
()[source]¶ Get pymongo Collection instance.
Return type: Collection 中文文档
获得pymongo.Collection的实例。
-
classmethod
col
()[source]¶ Alias of
collection()
Return type: Collection
-
classmethod
db
()[source]¶ Alias of
database()
Return type: Database
-
classmethod
smart_insert
(data, minimal_size=5, n_insert=0, n_skipped=0)[source]¶ An optimized Insert strategy.
中文文档
在Insert中, 如果已经预知不会出现IntegrityError, 那么使用Bulk Insert的速度要 远远快于逐条Insert。而如果无法预知, 那么我们采用如下策略:
- 尝试Bulk Insert, Bulk Insert由于在结束前不Commit, 所以速度很快。
- 如果失败了, 那么对数据的条数开平方根, 进行分包, 然后对每个包重复该逻辑。
- 若还是尝试失败, 则继续分包, 当分包的大小小于一定数量时, 则使用逐条插入。
直到成功为止。该Insert策略在内存上需要额外的 sqrt(nbytes) 的开销, 跟原数据相比体积很小。 但时间上是各种情况下平均最优的。
-
classmethod
smart_update
(data, upsert=False, _insert_after_update=False)[source]¶ Batch update with a lots orm data model.
Note
The batch update operation is not atomic. It can be done with transaction in MongoDB 4.0 +
Parameters: _insert_after_update – for developer use only, if True, will collect all to-insert document and bulk insert it at once after update. Return type: Tuple[int, int]
-
classmethod
by_id
(_id)[source]¶ Get one document instance by _id.
Return type: ExtendedDocument 中文文档
根据_id, 返回一条文档。
-
classmethod
by_filter
(filters)[source]¶ Filter objects by pymongo dict query.
Return type: QuerySet 中文文档
使用pymongo的API进行查询。
-
classmethod
random_sample
(filters=None, n=5)[source]¶ Randomly select n samples.
Parameters: Return type: List[ExtendedDocument]
中文文档
随机选择
n
个样本。
-