”;
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. |
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)
Note that Cleaning is only called if validation is turned on and when calling save().
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 |
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.
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. |
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).
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 |
”;