MongoEngine – Document Class

MongoEngine – Document Class ”; Previous Next 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() Print Page Previous Next Advertisements ”;

MongoEngine – Querying Database

MongoEngine – Querying Database ”; Previous Next The connect() function returns a MongoClient object. Using list_database_names() method available to this object, we can retrieve number of databases on the server. from mongoengine import * con=connect(”newdb”) dbs=con.list_database_names() for db in dbs: print (db) It is also possible to obtain list of collections in a database, using list_collection_names() method. collections=con[”newdb”].list_collection_names() for collection in collections: print (collection) As mentioned earlier, the Document class has objects attribute that enable access to objects associated with the database. The newdb database has a products collection corresponding to Document class below. To get all documents, we use objects attribute as follows − from mongoengine import * con=connect(”newdb”) class products (Document): ProductID=IntField(required=True) Name=StringField() price=IntField() for product in products.objects: print (”ID:”,product.ProductID, ”Name:”,product.Name, ”Price:”,product.price) Output ID: 1 Name: Laptop Price: 25000 ID: 2 Name: TV Price: 50000 ID: 3 Name: Router Price: 2000 ID: 4 Name: Scanner Price: 5000 ID: 5 Name: Printer Price: 12500 Print Page Previous Next Advertisements ”;

MongoEngine – Fields

MongoEngine – Fields ”; Previous Next A MongoEngine document class has one or more attributes. Each attribute is an object of Field class. BaseField is the base class or all field types. The BaseField class constructor has the following arguments − BaseField(db_field, required, default, unique, primary_key) The db_field represents name of database field. The required parameter decides whether value for this field is required, default is false. The default parameter contains default value of this field The unique parameter is false by default. Set to true if you want this field to have unique value for each document. The primary_key parameter defaults to false. True makes this field primary key. There are a number of Field classes derived from BaseField. Numeric Fields IntField (32bit integer), LongField (64 bit integer), FloatField (floating point number) field constructors have min_value and max_value parameters. There is also DecimalField class. Value of this field’s object is a float whose precision can be specified. Following arguments are defined for DecimalField class − DecimalField(min_value, max_value, force_string, precision, rounding) min_value specifies minimum acceptable value max_value specifies maximum value the field can have force_string If True, value of this field is stored as a string precision limits the floating representation to number of digits rounding Number is rounded as per following predefined constants decimal.ROUND_CEILING (towards Infinity) decimal.ROUND_DOWN (towards zero) decimal.ROUND_FLOOR (towards -Infinity) decimal.ROUND_HALF_DOWN (to nearest with ties going towards zero) decimal.ROUND_HALF_EVEN (to nearest with ties going to nearest even integer) decimal.ROUND_HALF_UP (to nearest with ties going away from zero) decimal.ROUND_UP (away from zero) decimal.ROUND_05UP (away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero) Text Fields StringField object can store any Unicode value. You can specify min_length and max_length of the string in the constructor. URLField object is a StringField with capability to validate input as a URL. EmailField validates the string as a valid email representation. StringField(max-length, min_length) URLField(url_regex) EmailField(domain_whiltelist, allow_utf8_user, allow_ip_domain) The domain_whitelist argument contains list of invalid domains which you would not support. If set to True, allow_utf8_user parameter allows the string to contain UTF8 characters as a part of email. The allow_ip_domain parameter is false by default, but if true, it can be a valid IPV4 or IPV6 address. Following example uses numeric and string fields − from mongoengine import * connect(”studentDB”) class Student(Document): studentid = StringField(required=True) name = StringField() age=IntField(min_value=6, max-value=20) percent=DecimalField(precision=2) email=EmailField() s1=Student() s1.studentid=”001” s1.name=”Mohan Lal” s1.age=20 s1.percent=75 s1.email=”[email protected]” s1.save() When above code is executed, the student collection shows a document as below − ListField This type of field wraps any standard field, thus allowing multiple objects to be used as a list object in a database. This field can be used with ReferenceField to implement one to many relationships. The student document class from above example is modified as below − from mongoengine import * connect(”studentDB”) class Student(Document): studentid = StringField(required=True) name = StringField(max_length=50) subjects = ListField(StringField()) s1=Student() s1.studentid=”A001” s1.name=”Mohan Lal” s1.subjects=[”phy”, ”che”, ”maths”] s1.save() The document added is shown in JSON format as follows − { “_id”:{“$oid”:”5ea6a1f4d8d48409f9640319″}, “studentid”:”A001″, “name”:”Mohan Lal”, “subjects”:[“phy”,”che”,”maths”] } DictField An object of DictField class stores a Python dictionary object. In the corresponding database field as well, this will be stored. In place of ListField in the above example, we change its type to DictField. from mongoengine import * connect(”studentDB”) class Student(Document): studentid = StringField(required=True) name = StringField(max_length=50) subjects = DictField() s1=Student() s1.studentid=”A001” s1.name=”Mohan Lal” s1.subjects[”phy”]=60 s1.subjects[”che”]=70 s1.subjects[”maths”]=80 s1.save() Document in the database appears as follows − { “_id”:{“$oid”:”5ea6cfbe1788374c81ccaacb”}, “studentid”:”A001″, “name”:”Mohan Lal”, “subjects”:{“phy”:{“$numberInt”:”60″}, “che”:{“$numberInt”:”70″}, “maths”:{“$numberInt”:”80″} } } ReferenceField A MongoDB document can store reference to another document using this type of field. This way, we can implement join as in RDBMS. A ReferenceField constructor uses name of other document class as parameter. class doc1(Document): field1=StringField() class doc2(Document): field1=StringField() field2=ReferenceField(doc1) In following example, StudentDB database contains two document classes, student and teacher. Document of Student class contains reference to an object of teacher class. from mongoengine import * connect(”studentDB”) class Teacher (Document): tid=StringField(required=True) name=StringField() class Student(Document): sid = StringField(required=True) name = StringField() tid=ReferenceField(Teacher) t1=Teacher() t1.tid=”T1” t1.name=”Murthy” t1.save() s1=Student() s1.sid=”S1” s1.name=”Mohan” s1.tid=t1 s1.save() Run above code and verify result in Compass GUI. Two collections corresponding to two document classes are created in StudentDB database. The teacher document added is as follows − { “_id”:{“$oid”:”5ead627463976ea5159f3081″}, “tid”:”T1″, “name”:”Murthy” } The student document shows the contents as below − { “_id”:{“$oid”:”5ead627463976ea5159f3082″}, “sid”:”S1″, “name”:”Mohan”, “tid”:{“$oid”:”5ead627463976ea5159f3081″} } Note that ReferenceField in Student document stores _id of corresponding Teacher document. When accessed, Student object is automatically turned into a reference, and dereferenced when corresponding Teacher object is accessed. To add reference to document being defined, use ‘self’ instead of other document class as argument to ReferenceField. It may be noted that use of ReferenceField may cause poor performance as far retrieval of documents is concerned. The ReferenceField constructor also has one optional argument as reverse_delete_rule. Its value determines what to be done if the referred document is deleted. The possible values are as follows − DO_NOTHING (0) – don’t do anything (default). NULLIFY (1) – Updates the reference to null. CASCADE (2) – Deletes the documents associated with the reference. DENY (3) – Prevent the deletion of the reference object. PULL (4) – Pull the reference from a ListField of references You can implement one to many relationship using list of references. Assuming that a student document has to be related with one or more teacher documents, the Student class must

MongoEngine – MongoDB Compass

MongoEngine – MongoDB Compass ”; Previous Next MongoDB has also developed a GUI tool for handling MongoDB databases. It is called MongoDB Compass. It is a convenient tool for performing all CRUD operations without manually writing queries. It helps in many activities such as indexing, document validation, etc. Download community edition of MongoDB Compass from https://www.mongodb.com/download-center/compass and start MongoDBCompassCommunity.exe (Ensure that MongoDB server is running before starting Compass). Connect to the local server by giving correct host and port number. All the databases currently available will be listed as below − Click on + button (shown at the bottom of left panel) to create new database. Choose name of database from list and select a Collection as shown below − You can add document directly or import from CSV or JSON file. Choose Insert Document from Add data drop down. Documents added will be displayed in JSON, list or tabular form − Note that, just as a table in relational database has a primary key, document in MongoDB database has a special key called “_id” that is automatically generated. MongoDB Inc. provides a Python driver for connection with MongoDB databases. It is called PyMongo whose usage is similar to standard SQL queries. After installing PyMongo module, we need object of MongoClient class for interacting with MongoDB server. <<< from pymongo import MongoClient <<< client=MongoClient() New database is created with the following statement − db=client.mydatabase CRUD operations on this database are performed with methods such as insert_one() (or insert_many()), find(), update() and delete() methods. Detailed discussion of PyMongo library is available at https://www.tutorialspoint.com/python_data_access/python_mongodb_introduction.htm. However, Python’s user defined objects cannot be stored in database unless it is converted in MongoDB’s data types. This is where we need MongoEngine library. Print Page Previous Next Advertisements ”;

MongoEngine – MongoDB

MongoEngine – MongoDB ”; Previous Next NoSQL databases have seen rise in popularity in the last decade. In today’s world of real time web applications, huge amount of data is being generated with mobile and embedded devices. Traditional relational databases (like Oracle, MySQL, etc.) are not suitable for strings. The processing of such data is also difficult as they have fixed and predefined schema, and are not scalable. NOSQL databases have flexible schema and are stored in distributed manner on a large number of community servers. NOSQL databases are classified on the basis of organization of data. MongoDB is a popular Document Store NOSQL database. Fundamental constituent of a MongoDB database is called a document. A document is a collection of key-value pairs stored in JSON format. More than one documents are stored in a collection. A collection can be considered as analogous to a table in any relational database, and a Document as row in a table. However, it should be noted that since MongoDB is schema less, number of key-value pairs in each document of a Collection need not be the same. MongoDB is developed by MongoDB Inc. It is a general-purpose, distributed document based database. It is available in enterprise as well as community edition. Latest version of Community version for Windows operating system can be downloaded from https://fastdl.mongodb.org/win32/mongodb-win32-x86_64-2012plus-4.2.6-signed.msi. Install MongoDB in a folder of your choice and start the server with the following command− D:mongodbbin>mongod Server is now ready for incoming connection requests at port 27017. MongoDB databases are stored in bin/data directory. This location can be changed by –dbpath option in above command. In another command terminal, start MongoDB console with the following command − D:mongodbbin>mongo MongoDB prompt is similar to what we normally see in MySQL or SQLite terminal. All database operations such as creating database, inserting a document, updating and deleting as well as retrieval of documents can be done from within the console. E:mongodbbin>mongo MongoDB shell version v4.0.6 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { “id” : UUID(“0d848b11-acf7-4d30-83df-242d1d7fa693″) } MongoDB server version: 4.0.6 — > Default database in use is test. > db Test With ”use” command any other database is set as current. If the named database does not exist, new one is created. > use mydb switched to db mydb Please refer to our detailed tutorial on MongoDB at https://www.tutorialspoint.com/mongodb/index.htm. Print Page Previous Next Advertisements ”;

MongoEngine – Connecting to MongoDB Database

Connecting to MongoDB Database ”; Previous Next As mentioned earlier, you should first start MongoDB server using mongod command. MongoEngine provides connect() function to connect to a running instance of mongodb server. from mongoengine import connect connect(‘mydata.db’) By default, MongoDB server is running on localhost and on port 27017. To customize, you should provide the host and port arguments to connect() − connect(”mydata.db”, host=”192.168.1.1”, port=12345) In case the database requires authentication, its credentials such as username, password and authentication_source arguments should be provided. connect(”mydata.db”, username=”user1”, password=”***”, authentication_source=”admin”) MongoEngine also supports URI style connections instead of IP address. connect(”mydata.db”, host=”mongodb://localhost/database_name”) The connect() function has another optional parameter called replicaset. MongoDB is a distributed database. Data stored in one server is usually replicated in many server instances in order to ensure high availability. A replica set in MongoDB is a group of mongod processes on which the same data set is maintained. Replica sets are the basis for all production deployments. connect(host=”mongodb://localhost/dbname?replicaSet=rs-name”) Following replica set methods are defined as follows: rs.add() Adds a member to a replica set. rs.conf() Returns the replica set configuration document. rs.freeze() Prevents the current member from seeking election as primary for a period of time. rs.initiate() Initializes a new replica set. rs.reconfig() Re-configures a replica set by applying a new replica set configuration object. rs.remove() Removes a member from a replica set. MongoEngine also allows connection with multiple databases. You need to provide unique alias name for each database. For example, following code connects Python script to two MongoDB databases. connect(alias=”db1”, db=”db1.db”) connect(alias=”db2”, db=”db2.db”) Print Page Previous Next Advertisements ”;

MongoEngine – Object Document Mapper

MongoEngine – Object Document Mapper ”; Previous Next MongoDB is a document based database. Each document is a JSON like representation of fields and values. A document in MongoDB is roughly equivalent to a row in RDBMS table (MongoDB equivalent of table is Collection). Even though MongoDB does not enforce any predefined schema, the field objects in a document have certain data type. MongoDB data types are very much similar to Python’s primary data types. If one has to store object of Python’s user defined class, its attributes have to be manually parsed to equivalent MongoDB data types. MongoEngine provides a convenient abstraction layer over PyMongo and maps each object of Document class to a document in MongoDB database. MongoEngine API has been developed by Hary Marr in August 2013. Latest version of MongoEngine is 0.19.1. MongoEngine is to MongoDB what SQLAlchemy is to RDBMS databases. MongoEngine library provides a Document class that is used as base for defining custom class. Attributes of this class form the fields of MongoDB document. The Document class defines methods to perform CRUD operations. In subsequent topics, we shall learn how to use them. Print Page Previous Next Advertisements ”;

MongoEngine – Home

MongoEngine Tutorial PDF Version Quick Guide Resources Job Search Discussion MongoEngine is a Python library that acts as an Object Document Mapper with MongoDB, a NOSQL database. It is similar to SQLAlchemy, which is the Object Relation Mapper (ORM) for SQL based databases. Audience In this tutorial, you will learn to perform CRUD operations on a MongoDB database with the help of MongoEngine and Python. Prerequisites This tutorial is useful for Python professionals who intend to use MongoDB database in their applications. Knowledge of Python and object oriented concepts are essential. Print Page Previous Next Advertisements ”;

MongoEngine – Filters

MongoEngine – Filters ”; Previous Next The objects attribute is a QuerySet manager. It creates and returns a QuerySet when accessed. A query can be subjected to filter with the help of field names as keyword arguments. For example, from above products collection, to print details of document with name of product as ‘TV’, we use Name as keyword argument. for product in products.objects(Name=”TV”): print (”ID:”,product.ProductID, ”Name:”,product.Name, ”Price:”,product.price) You can use filter method of QuerySet object to apply filter to query. Following code snippet also returns product details with name=’TV’. qset=products.objects for product in qset.filter(Name=”TV”): print (”ID:”,product.ProductID, ”Name:”,product.Name, ”Price:”,product.price) Print Page Previous Next Advertisements ”;