- MongoEngine Tutorial
- MongoEngine - Home
- MongoEngine - MongoDB
- MongoEngine - MongoDB Compass
- MongoEngine - Object Document Mapper
- MongoEngine - Installation
- MongoEngine - Connecting to MongoDB Database
- MongoEngine - Document Class
- MongoEngine - Dynamic Schema
- MongoEngine - Fields
- MongoEngine - Add/Delete Document
- MongoEngine - Querying Database
- MongoEngine - Filters
- MongoEngine - Query Operators
- MongoEngine - QuerySet Methods
- MongoEngine - Sorting
- MongoEngine - Custom Query Sets
- MongoEngine - Indexes
- MongoEngine - Aggregation
- MongoEngine - Advanced Queries
- MongoEngine - Document Inheritance
- MongoEngine - Atomic Updates
- MongoEngine - Javascript
- MongoEngine - GridFS
- MongoEngine - Signals
- MongoEngine - Text Search
- MongoEngine - Extensions
- MongoEngine Useful Resources
- MongoEngine - Quick Guide
- MongoEngine - Useful Resources
- MongoEngine - Discussion
MongoEngine - Document Class
MongoEngine is termed as ODM (Object Document Mapper). MongoEngine defines a Document class. This is a base class whose inherited class is used to define structure and properties of collection of documents stored in MongoDB database. Each object of this subclass forms Document in Collection in database.
Attributes in this Document subclass are objects of various Field classes. Following is an example of a typical Document class −
from mongoengine import * class Student(Document): studentid = StringField(required=True) name = StringField(max_length=50) age = IntField() def _init__(self, id, name, age): self.studentid=id, self.name=name self.age=age
This appears similar to a model class in SQLAlchemy ORM. By default, name of Collection in database is the name of Python class with its name converted to lowercase. However, a different name of collection can be specified in meta attribute of Document class.
meta={collection': 'student_collection'}
Now declare object of this class and call save() method to store the document in a database.
s1=Student('A001', 'Tara', 20) s1.save()