<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Isuru Maldeniya on Medium]]></title>
        <description><![CDATA[Stories by Isuru Maldeniya on Medium]]></description>
        <link>https://medium.com/@isuru_maldeniya?source=rss-f2cf503cb9dd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*f0WgxYVZxFEZFQqlM-OYag.jpeg</url>
            <title>Stories by Isuru Maldeniya on Medium</title>
            <link>https://medium.com/@isuru_maldeniya?source=rss-f2cf503cb9dd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 00:19:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@isuru_maldeniya/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Integrating MongoDB with Python]]></title>
            <link>https://medium.com/@isuru_maldeniya/integrating-mongodb-with-python-867d53e923d9?source=rss-f2cf503cb9dd------2</link>
            <guid isPermaLink="false">https://medium.com/p/867d53e923d9</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[python3]]></category>
            <dc:creator><![CDATA[Isuru Maldeniya]]></dc:creator>
            <pubDate>Tue, 14 Aug 2018 06:01:20 GMT</pubDate>
            <atom:updated>2018-08-14T07:34:27.546Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vMXuornziakWS-kMU-1A8w.jpeg" /></figure><p>This tutorial is about how to use python with MongoDB in your application. I hope you will learn something from this tutorial. At the end of this tutorial, you will know how to create data models, to create collections and documents in MongoDB, how to access data in MongoDB with python. <br>MongoDB is a popular non-relational database that we can use to store data in our applications. I suggest before reading this tutorial you should at least have the basic knowledge of what is a collection, a document in MongoDB and have a basic knowledge of python. In the bottom of the tutorial you can find the official documentation if you are new to MongoDB and python.</p><h3>Prerequisite</h3><ol><li>Python 3.6</li><li>pip3 — python package manager which we can use to install various packages</li><li>MongoDB server install and running</li><li>MongoDB compass [optional]</li><li>Preferred text editor. (VS code/ Atom etc)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ux82YV91iyVtgHIiuSKzAA.png" /></figure><p>There are many tools in python that we can use to work with MongoDB. Most popular ones are <strong><em>pymongo</em></strong> and <strong><em>pymodm</em></strong>. As I deal with various situations in MongoDB I think when creating data models and saving data to database pymodm is better than pymongo. However when accessing the data from the database pymongo is better than the pymodm. So why not use both of them in your application?</p><p>First let’s learn how to install pymongo and pymodm.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/275/1*nhBULie6HFogMTSiiNwXIA.png" /></figure><p>Since I am using python 3.6 I am using pip3 to install new packages.</p><p>Now we are ready to use pymongo and pymodm in our application.</p><p>First create a connection to the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*_D0qYKUFxjxCrODRMfnupw.png" /></figure><p>If you don’t have database called <em>myDatabase</em> in your local after running this python file it will automatically create database called <em>myDatabase</em>. In here I have assigned a variable to our database when I am connecting to MongoDB. This <strong><em>db</em></strong> variable will be used in the future to access the data in the database in this tutorial.</p><p>When you connect to your database you need to define the port which you will run your MongoDB server(default port is 27017).</p><p>Start MongoDB server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*hqCfxYg9klKTYdSR_0ODtQ.png" /><figcaption>start the MongoDB server and check it’s status</figcaption></figure><p>So after connecting to the database let’s create some models to save data in our database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*tiV2oH-0Q3qmm2pytRMkpA.png" /><figcaption>creating models</figcaption></figure><p>I have used <strong><em>pymodm</em></strong> to create models which will act as collection in the database since it is the easiest way to make models. Here actor model and film model inherit from Mongomodel(inheritance is an important object oriented concept) which will create the actor, film collections. Here character filed can refer to a string and list field is a python list.When I am creating the actor model I have replaced the default <strong><em>_id</em></strong> filed that will generate automatically in MongoDB by actor name. So I am not allowed to create actors with the same name under collection actor. Same manner when I am creating film model I have used film name as the primary key for that collection which will replace the default <strong><em>_id</em></strong> field in the documents that will be created under films collection. Film model also consist of an actor which is an instance of the actor model. Note that even though we have use name fields, when they are saving to the database it goes under <strong><em>_id</em></strong> field since we declare it as the primary key.</p><p>Now let’s save some data to the database using models we have created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Oodz9qDMduoVtHF7KYIoxw.png" /><figcaption>saving data to the database</figcaption></figure><p>I have created 2 actors and a film which will use reference field as the actor1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*-hVTS8jvCf4LdWa2izvYGA.png" /></figure><p>Below you can see how the documents saved in the database. I am using MongoDB compass to manage and view my MongoDB databases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*_FxIuork_Aa5X3mY_7d3Ww.png" /><figcaption>actor collection</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*cZ7vvgDo4Ab1b3DlIaaFig.png" /><figcaption>film collection</figcaption></figure><p>Here the important thing to remember is that <strong><em>.save()</em></strong> will save the document that we created using above models to the database.</p><p>Now let’s learn how to access the data in the database. I will be using <strong><em>pymongo</em></strong> for this purpose. Below are two functions that can be used to find any film or actor document by their name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/865/1*PZDQ-EfspIL9J6fO_4cYDQ.png" /><figcaption>finding documents</figcaption></figure><p>Note that here we have to find documents by it’s <strong><em>_id </em></strong>since we declare the name as the <strong><em>_id</em></strong> field.</p><p>Now let’s print all the actor in the actor collection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*Hl_Inf1NsH79YxcKV6gWEg.png" /><figcaption>finding all the actors in the database</figcaption></figure><p>The result is as follows.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*C4EFD5s5OQAG89U4stc1_g.png" /></figure><p>Now, let’s update actor <strong>Jackie Chan</strong> document by adding another film to his films_acted field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*6EWusKuPESRR2oTW5ZG4-g.png" /><figcaption>update an existing field</figcaption></figure><p>First, we need to find <strong>Jackie Chan</strong> in our database. Then we need to grab the required field that we need to update. Since films_acted field is a list I just need to append the new film that I need to add. Then I have used the update() function to update the <strong>Jackie Chan</strong> document. Now <strong>Jackie Chan </strong>will be like this in our database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/821/1*Bq6pc_ywkhVs6ib68ONLcw.png" /><figcaption>new Jackie Chan document</figcaption></figure><p>Now, let’s learn how to create a new field to existing document. I will add a new field to film <strong>Specter</strong> document.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/867/1*xEGbtVTS-jO1YkNPfksL1A.png" /><figcaption>creating new field to film specter.</figcaption></figure><p>Since film document is a dictionary I have used update() instead of append. After adding the new field to the document again we need to update the database by running update() function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/542/1*q8AODml8UtTZoGYCuwaUrA.png" /><figcaption>new specter document</figcaption></figure><p>So likewise you can update any document as you wish by using <strong><em>pymongo</em></strong> tool. If you want to delete a document first find the specific document from the database. Then run the delete() function as below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/877/1*3ZIJTmeiX_O3q9lKc0EDiA.png" /><figcaption>deleting an actor</figcaption></figure><p>I hope now you have a basic idea about how to use <strong><em>MongoDB</em></strong> with <strong><em>python</em></strong> in your application. 😇</p><p>Happy Coding!!! 😃</p><p><strong><em>MongoDB</em></strong> official documentation — <a href="https://docs.mongodb.com/">https://docs.mongodb.com/</a></p><p><strong><em>Python</em></strong> 3.6 official documentation — <a href="https://docs.python.org/3.6/">https://docs.python.org/3.6/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=867d53e923d9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>