- MongoEngine - Discussion
- MongoEngine - Useful Resources
- MongoEngine - Quick Guide
- MongoEngine - Extensions
- MongoEngine - Text Search
- MongoEngine - Signals
- MongoEngine - GridFS
- MongoEngine - Javascript
- MongoEngine - Atomic Updates
- MongoEngine - Document Inheritance
- MongoEngine - Advanced Queries
- MongoEngine - Aggregation
- MongoEngine - Indexes
- MongoEngine - Custom Query Sets
- MongoEngine - Sorting
- MongoEngine - QuerySet Methods
- MongoEngine - Query Operators
- MongoEngine - Filters
- MongoEngine - Querying Database
- MongoEngine - Add/Delete Document
- MongoEngine - Fields
- MongoEngine - Dynamic Schema
- MongoEngine - Document Class
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Installation
- MongoEngine - Object Document Mapper
- MongoEngine - MongoDB Compass
- MongoEngine - MongoDB
- MongoEngine - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
MongoEngine - Add/Delete Document
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. |
vapdate | vapdates the document; set to False to skip. |
clean | call the document clean method, vapdate 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 pst 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 vapdation of documents before calpng save(). By providing a custom clean() method, you can do any pre vapdation/data cleaning.
class MyDocument(Document): ... ... def clean(self): if <condition>==True: msg = error message. raise VapdationError(msg)
Note that Cleaning is only called if vapdation is turned on and when calpng save().
Document class also has insert() method to perform bulk insert. It has following parameters −
doc_or_docs | A document or pst of documents to be inserted |
load_bulk | If True, returns the pst 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 calpng 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 |