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()
”;