Mongoengine 简明教程

MongoEngine - Add/Delete Document

我们已使用 Document 类的 save() 方法在集合中添加了一个文档。还可以使用以下参数进一步自定义 save() 方法:

We have already used save() method of Document class to add a document in the collection. The save() method can be further customized with the help of following arguments −

force_insert

Default is False, if set to True doesn’t allow updates of existing documents.

validate

validates the document; set to False to skip.

clean

call the document clean method, validate argument should be True.

write_concern

will be used as options for the resultant getLastError command. For example, save(…​, write_concern={w: 2, fsync: True}, …​) will wait until at least two servers have recorded the write and will force an fsync on the primary server.

cascade

Sets the flag for cascading saves. You can set a default by setting “cascade” in the document meta.

cascade_kwargs

optional keyword arguments to be passed throw to cascading saves. Equivalent to cascade=True.

_refs

A list of processed references used in cascading saves

save_condition

only perform save if matching record in db satisfies condition(s). Raises OperationError if the conditions are not satisfied

signal_kwargs

kwargs dictionary to be passed to the signal calls.

在调用保存之前,您可以设置用于文档验证的清理规则。通过提供自定义 clean() 方法,您可以执行任何预验证/数据清理。

You can set cleaning rules for validation of documents before calling save(). By providing a custom clean() method, you can do any pre validation/data cleaning.

class MyDocument(Document):
   ...
   ...

   def clean(self):
      if <condition>==True:
         msg = 'error message.'
         raise ValidationError(msg)

请注意,只有在启用验证时才会调用 Cleaning,并且在调用 save() 时也会调用。

Note that Cleaning is only called if validation is turned on and when calling save().

Document 类还具有 insert() 方法来执行批量插入。它具有以下参数:

Document class also has insert() method to perform bulk insert. It has following parameters −

doc_or_docs

A document or list of documents to be inserted

load_bulk

If True, returns the list of document instances

write_concern

Extra keyword arguments are passed down to insert() which will be used as options for the resultant getLastError command.

signal_kwargs

(optional) kwargs dictionary to be passed to the signal calls

如果文档包含任何 ReferenceField 对象,则默认情况下 save() 方法不会保存对这些对象的任何更改。如果您希望保存所有引用,而不会注意到每个 save 都是一个单独的查询,那么将 cascade 作为 True 传递给 save 方法将级联任何保存。

If document contains any ReferenceField objects, then by default the save() method will not save any changes to those objects. If you want all references to be saved also, noting each save is a separate query, then passing cascade as True to the save method will cascade any saves.

通过调用 delete() 方法从集合中删除文档非常容易。请记住,只有在之前已保存文档时此操作才会生效。delete() 方法具有以下参数:

Deleting a document from its collection is very easy, by calling delete() method. Remember that it will only take effect if the document has been previously saved. The delete() method has following arguments −

signal_kwargs

(optional) kwargs dictionary to be passed to the signal calls.

write_concern

Extra keyword arguments are passed down which will be used as options for the resultant getLastError command.

要从数据库中删除整个集合,请使用 drop_collecction() 方法。它从数据库中删除与此 Document 类型关联的整个集合。如果未设置文档集合(例如,如果它是抽象的),则该方法会引发 OperationError。

To delete entire collection from database use drop_collecction() method. It drops the entire collection associated with this Document type from the database. The method raises OperationError if the document has no collection set (i.g. if it is abstract).

文档类中的 modify() 方法在数据库中执行文档的原子更新并重新加载其更新版本。如果文档已更新,则返回 True;如果数据库中的文档与查询不匹配,则返回 False。请注意,如果该方法返回 True,则对该文档所做的所有尚未保存的更改将被拒绝。

The modify() method in document class performs atomic update of the document in the database and reloads its updated version. It returns True if the document has been updated or False if the document in the database does not match the query. Note that all unsaved changes that have been made to the document are rejected if the method returns True.

Parameters

query

The update will be performed only if the document in the database matches the query

update

Django-style update keyword arguments