{"id":10308,"date":"2019-11-04T08:30:36","date_gmt":"2019-11-04T07:30:36","guid":{"rendered":"https:\/\/rubikscode.net\/?p=10308"},"modified":"2019-11-03T19:05:40","modified_gmt":"2019-11-03T18:05:40","slug":"using-mongodb-in-python","status":"publish","type":"post","link":"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/","title":{"rendered":"Using MongoDB in Python"},"content":{"rendered":"\n<p>In our <a rel=\"noreferrer noopener\" aria-label=\"Guide to MongoDB (opens in a new tab)\" href=\"https:\/\/www.educative.io\/collection\/10370001\/5227455105204224?utm_source=sendgrid&amp;utm_medium=email&amp;utm_campaign=mongodb&amp;utm_content=button&amp;utm_source=Newsletter&amp;utm_medium=email&amp;utm_content=The+Definitive+Guide+to+MongoDB&amp;utm_campaign=%5BEU+%2B+AFR%5D+MongoDB\" target=\"_blank\"><strong>Definite Guide to MongoDB<\/strong><\/a>, we covered a lot of ground. There you can get more information about <strong>NoSQL <\/strong>databases, what they are, how they can be used, and which are the <strong>benefits <\/strong>of using one. Apart from that, you can find more information about different <strong>types <\/strong>of NoSQL databases and their most popular <strong>representatives<\/strong>. One of those types are so-called <strong>Document <\/strong>NoSQL databases and their most popular representative is <a rel=\"noreferrer noopener\" href=\"https:\/\/www.mongodb.com\/\" target=\"_blank\"><strong>MongoDB<\/strong><\/a>. <\/p>\n\n\n\n<p>So, we went on and covered some of the <strong>foundations <\/strong>of this database. There one can learn how to install <em>MongoDB<\/em>, create databases, collections, documents and how to use these entities. Also, there is a lot of information about <em>MongoDB <\/em><strong>deployment<\/strong>, shards, replica sets and how to manipulate them. You can find out how to <strong>use <\/strong>this database in .NET environment, in JavaScript MEAN framework and in <em>Serverless <\/em>environment too. So we wanted to build up on that and write an article that will cover how you can use <em>MongoDB <\/em>in <em><strong>Python<\/strong><\/em>.<\/p>\n\n\n\n<p>In this article, we implement one <strong>repository<\/strong>, using which you can manipulate data in the collection <em>Users<\/em>. Something similar we have done in .NET in the<strong> <\/strong><a rel=\"noreferrer noopener\" href=\"https:\/\/www.educative.io\/collection\/10370001\/5227455105204224?utm_source=sendgrid&amp;utm_medium=email&amp;utm_campaign=mongodb&amp;utm_content=button&amp;utm_source=Newsletter&amp;utm_medium=email&amp;utm_content=The+Definitive+Guide+to+MongoDB&amp;utm_campaign=%5BEU+%2B+AFR%5D+MongoDB\" target=\"_blank\"><strong>Definite Guide to MongoDB<\/strong><\/a>. In fact, we present <strong>two ways<\/strong> to achive that, ie. using two <em>Python<\/em> modules &#8211; <em>PyMongo <\/em>and <em>MongoEngine<\/em>. The implementation with first library <em>PyMongo<\/em> is<strong> low-level<\/strong> implementation, while the implementation with <em>MongoEngine<\/em> can be observed as <strong>higher-level<\/strong> implementation. However, before we get into nitty-gritty details of each implementation, let&#8217;s first <strong>install <\/strong><em>MongoDB<\/em> and both <em>Python<\/em> modules.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"333\" data-attachment-id=\"10385\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/1-1-7\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/1-1.png\" data-orig-size=\"795,333\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"1-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/1-1.png\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/1-1.png\" alt=\"\" class=\"wp-image-10385\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/1-1.png 795w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/1-1-480x201.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 795px, 100vw\" \/><\/figure>\n\n\n\n<p><em>MongoDB <\/em>is free and <strong>open-source<\/strong>. It can be downloaded from\u00a0<strong><a href=\"https:\/\/www.mongodb.com\/download-center?jmp=nav#community\">here<\/a><\/strong>. After picking up version and your <strong>operating system<\/strong>. In this example, we use <em>MongoDB 4.2.1 Comunity Server<\/em> for <em>Windows<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1200\" height=\"439\" data-attachment-id=\"10311\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/image-59\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image.png\" data-orig-size=\"1200,439\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1024x375.png\" src=\"https:\/\/i1.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image.png?fit=1024%2C375&amp;ssl=1\" alt=\"\" class=\"wp-image-10311\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image.png 1200w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-416x152.png 416w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-300x110.png 300w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-768x281.png 768w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1024x375.png 1024w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1080x395.png 1080w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-980x359.png 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-480x176.png 480w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-24x9.png 24w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-36x13.png 36w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-48x18.png 48w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><figcaption>MongoDB Download Page<\/figcaption><\/figure><\/div>\n\n\n\n<p>After installation, a\u00a0user can run <em>MongoDB <\/em>server by using the <strong>command<\/strong>:<\/p>\n\n\n\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> mongod &#8211;dbpath PATH_TO_THE_DIR<\/p>\n\n\n\n<p>With this installation, <em>MongoCompass<\/em> is installed as well. That is visual <strong>GUI <\/strong>component, using which you can manipulate your databases, collections and documents:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1258\" height=\"459\" data-attachment-id=\"10332\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/image-1-5\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1.png\" data-orig-size=\"1258,459\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-1024x374.png\" src=\"https:\/\/i1.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1.png?fit=1024%2C374&amp;ssl=1\" alt=\"\" class=\"wp-image-10332\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1.png 1258w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-300x109.png 300w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-768x280.png 768w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-1024x374.png 1024w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-1080x394.png 1080w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-980x358.png 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-480x175.png 480w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-24x9.png 24w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-36x13.png 36w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-48x18.png 48w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1-600x219.png 600w\" sizes=\"auto, (max-width: 1258px) 100vw, 1258px\" \/><figcaption>MongoDB Compass<\/figcaption><\/figure><\/div>\n\n\n\n<p>For the purpose of these examples, we use <em>local<\/em> database and <em>user<\/em> collection:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1251\" height=\"429\" data-attachment-id=\"10334\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/image-2-5\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2.png\" data-orig-size=\"1251,429\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"image-2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-1024x351.png\" src=\"https:\/\/i0.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2.png?fit=1024%2C351&amp;ssl=1\" alt=\"\" class=\"wp-image-10334\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2.png 1251w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-300x103.png 300w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-768x263.png 768w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-1024x351.png 1024w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-1080x370.png 1080w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-980x336.png 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-480x165.png 480w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-24x8.png 24w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-36x12.png 36w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-48x16.png 48w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2-600x206.png 600w\" sizes=\"auto, (max-width: 1251px) 100vw, 1251px\" \/><figcaption>&#8216;User&#8217; Collection<\/figcaption><\/figure><\/div>\n\n\n\n<p>As we mentioned you are going to need two <em>Python<\/em> modules, so let&#8217;s install them. First you can install <em>PyMongo<\/em> like this:<\/p>\n\n\n\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> pip install pymongo<\/p>\n\n\n\n<p><br>After that <em>MongoEngine<\/em>:<\/p>\n\n\n\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\">pip install mongoengine<\/p>\n\n\n\n<p><br>And that is pretty much it. We are ready for implementations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PyMongo Implementation<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"333\" data-attachment-id=\"10386\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/2-1-7\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/2-1.png\" data-orig-size=\"795,333\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"2-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/2-1.png\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/2-1.png\" alt=\"\" class=\"wp-image-10386\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/2-1.png 795w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/2-1-480x201.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 795px, 100vw\" \/><\/figure>\n\n\n\n<p>Let&#8217;s implement <em>UserRepository<\/em> class is using <em>PyMongo <\/em>module. To be more precise, we need just one <strong>class <\/strong>from this module &#8211; <em>MongoClient<\/em>:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204207\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-import_pymongo-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"import_pymongo.py content, created by NMZivkovic on 12:22PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"import_pymongo.py\">\n        <tr>\n          <td id=\"file-import_pymongo-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-import_pymongo-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">from pymongo import MongoClient<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/277335f96b707a82a00d183916382e66\/raw\/15bf9c185c466fed6c2e73a5b905adc33029768f\/import_pymongo.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/277335f96b707a82a00d183916382e66#file-import_pymongo-py\" class=\"Link--inTextBlock\">\n          import_pymongo.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>In the constructor of the <em>UserRepository<\/em>, we create <strong>instance <\/strong>of <em>MongoClient<\/em>  and connect to the cluster. There are several ways you can achieve that, and you can see two ways in the code. Then you pick database and collection. The rest of the implementation is more less just <strong>wrapping <\/strong>functions from <em>self._user<\/em> field.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204212\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_constructor-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_constructor.py content, created by NMZivkovic on 12:23PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_constructor.py\">\n        <tr>\n          <td id=\"file-repository_constructor-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_constructor-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class UserRepository(object):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_constructor-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    def __init__(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_constructor-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">        mongoclient = MongoClient(&#39;localhost&#39;, 27017)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_constructor-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">        #mongoclient = MongoClient(&#39;mongodb:\/\/localhost:27017&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_constructor-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_constructor-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">        database = mongoclient.local<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_constructor-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_constructor-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">        self._users = database.user<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/28b1ff2c72570653319e59f3cbe75b85\/raw\/fb62129ebeaed88fd2d4815190bee2bd3538b967\/repository_constructor.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/28b1ff2c72570653319e59f3cbe75b85#file-repository_constructor-py\" class=\"Link--inTextBlock\">\n          repository_constructor.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>Now, we are able to <strong>create <\/strong>an object of our class like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204226\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_instance-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_instance.py content, created by NMZivkovic on 12:23PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_instance.py\">\n        <tr>\n          <td id=\"file-repository_instance-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_instance-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user_repo = UserRepository()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/4442256790852affdb0efdd924446405\/raw\/ad6bcbf8be13ed82bf5cd69420d42937790c1c84\/repository_instance.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/4442256790852affdb0efdd924446405#file-repository_instance-py\" class=\"Link--inTextBlock\">\n          repository_instance.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<h3 class=\"wp-block-heading\">Create Operations<\/h3>\n\n\n\n<p><br>Next, we <strong>implement <\/strong><em>create<\/em> operations. We add two methods: <em>insert<\/em> and <em>insert_many<\/em>. First method adds just <strong>one <\/strong><em>user<\/em> to the collection, while the other adds all the users from the provided <strong>array<\/strong>. We can see that underneath they are just utilizing functions <em>insert_one<\/em> and <em>insert_many<\/em> from <em>PyMongo<\/em>.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204235\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_create-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_create.py content, created by NMZivkovic on 12:24PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_create.py\">\n        <tr>\n          <td id=\"file-repository_create-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_create-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class UserRepository(object):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_create-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    def __init__(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_create-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">        mongoclient = MongoClient(&#39;localhost&#39;, 27017)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_create-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">        #mongoclient = MongoClient(&#39;mongodb:\/\/localhost:27017&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_create-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_create-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">        database = mongoclient.local<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_create-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">        self._users = database.user<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_create-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-repository_create-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">    # Create operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-repository_create-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    def insert(self, user):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-repository_create-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_one(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-repository_create-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-repository_create-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    def insert_many(self, users):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-repository_create-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_many(users)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/8d08ea593b8cd47c856bc4ed58f95d00\/raw\/d0f70b9ddd746b79abbcf5ac36d13a3f439d859a\/repository_create.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/8d08ea593b8cd47c856bc4ed58f95d00#file-repository_create-py\" class=\"Link--inTextBlock\">\n          repository_create.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>Now, we can use these <strong>functions <\/strong>from our object. Method <em>insert<\/em> can be used like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204257\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_insert_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_insert_usage.py content, created by NMZivkovic on 12:26PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_insert_usage.py\">\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user = {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name&#39;:&quot;Rubik&quot;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">    &#39;age&#39;:33,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    &#39;blog&#39;:&quot;rubikscode.net&quot;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.insert(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_insert_usage-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_insert_usage-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/58279de6f02f465daf18157b707bfdca\/raw\/ec78c6f0b7eafd43f6246505c494c5a236031227\/repository_insert_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/58279de6f02f465daf18157b707bfdca#file-repository_insert_usage-py\" class=\"Link--inTextBlock\">\n          repository_insert_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>When we <strong>peek <\/strong>into this collection using <em>MongoDB Compass<\/em> we can see that document is created in <em>user<\/em> collection.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1002\" height=\"236\" data-attachment-id=\"10340\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage1\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1.jpg\" data-orig-size=\"1002,236\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1.jpg\" alt=\"\" class=\"wp-image-10340\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1.jpg 1002w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1-980x231.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1-480x113.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1002px, 100vw\" \/><figcaption>Created one document<\/figcaption><\/figure><\/div>\n\n\n\n<p>While <em>insert_many<\/em> can be used like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204408\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_create_many-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_create_many.py content, created by NMZivkovic on 12:36PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_create_many.py\">\n        <tr>\n          <td id=\"file-repository_create_many-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_create_many-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user_1 = {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_create_many-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name&#39;:&quot;Vanja&quot;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_create_many-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">    &#39;age&#39;:29,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_create_many-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    &#39;blog&#39;:&quot;eventroom.org&quot;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_create_many-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_create_many-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_create_many-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">user_2 = {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_create_many-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">    &#39;name&#39;:&quot;Marko&quot;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-repository_create_many-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">    &#39;age&#39;:36,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-repository_create_many-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    &#39;blog&#39;:&quot;rubikscode.net&quot;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-repository_create_many-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-repository_create_many-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-repository_create_many-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.insert_many([user_1, user_2])<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_create_many-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-repository_create_many-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/f5b9129a2f1d649dc481635831fa7827\/raw\/84615685d502800e6e4ab171f773f8fc89c2712d\/repository_create_many.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/f5b9129a2f1d649dc481635831fa7827#file-repository_create_many-py\" class=\"Link--inTextBlock\">\n          repository_create_many.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The result of this operation once again can be checked <strong>visually <\/strong>using <em>MongoDB Compass<\/em>:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"457\" data-attachment-id=\"10342\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage2\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2.jpg\" data-orig-size=\"1009,457\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2.jpg\" alt=\"\" class=\"wp-image-10342\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2.jpg 1009w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2-980x444.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2-480x217.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1009px, 100vw\" \/><figcaption>Created multiple documents<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Read Operations<\/h3>\n\n\n\n<p><br>Cool, now when we can add documents into our collection, let&#8217;s implement functions that are able to <strong>retrieve <\/strong>this information. We add three new functions: <em>read_all<\/em>, <em>read_many<\/em> and <em>read<\/em>. Function <em>read_all<\/em> returns <strong>all <\/strong>documents from the collection. Methods <em>read<\/em> and <em>read_many<\/em> are similar, they both accept some kind of <strong>conditions<\/strong>. The first one, however, returns just one document that <strong>satisfies <\/strong>the conditions, while other returns all documents that satisfy the condition.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204817\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_read_opertaions-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_read_opertaions.py content, created by NMZivkovic on 01:06PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_read_opertaions.py\">\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class UserRepository(object):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    def __init__(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">        mongoclient = MongoClient(&#39;localhost&#39;, 27017)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">        #mongoclient = MongoClient(&#39;mongodb:\/\/localhost:27017&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">        database = mongoclient.local<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">        self._users = database.user<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">    # Create operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    def insert(self, user):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_one(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    def insert_many(self, users):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_many(users)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">    # Read operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">    def read_all(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">    def read_many(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find(conditions)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC23\" class=\"blob-code blob-code-inner js-file-line\">    def read(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_read_opertaions-py-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-repository_read_opertaions-py-LC24\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find_one(conditions)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/2a72602609c7bef69a8a2b78211dc59b\/raw\/bdd8300e94ea39b690fcfce3ce31a6eace6a84b2\/repository_read_opertaions.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/2a72602609c7bef69a8a2b78211dc59b#file-repository_read_opertaions-py\" class=\"Link--inTextBlock\">\n          repository_read_opertaions.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>These functions can be <strong>used <\/strong>like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99204916\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-read_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"read_usage.py content, created by NMZivkovic on 01:14PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"read_usage.py\">\n        <tr>\n          <td id=\"file-read_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-read_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212;Read All&#8212;&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-read_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">all_users = user_repo.read_all()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-read_usage-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-read_usage-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">for user in all_users:<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-read_usage-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">    print(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-read_usage-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212;&#8212;&#8212;&#8212;&#8211;\\n&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-read_usage-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-read_usage-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-read_usage-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212;Read Many&#8211;&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-read_usage-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">rc_users = user_repo.read_many({&#39;blog&#39;:&#39;rubikscode.net&#39;})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-read_usage-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-read_usage-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">for user in rc_users:<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-read_usage-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    print(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-read_usage-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212;&#8212;&#8212;&#8212;&#8211;\\n&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-read_usage-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-read_usage-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212; Read &#8212;&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-read_usage-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">one_rc_user = user_repo.read({&#39;blog&#39;:&#39;rubikscode.net&#39;})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-read_usage-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-read_usage-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">print(one_rc_user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-read_usage-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-read_usage-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">print(&quot;&#8212;&#8212;&#8212;&#8212;&#8211;\\n&quot;)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/35c10e2206f2fdfa56ce5a9fe4450317\/raw\/40f916cf37fd0a5291c7b86f4b81c849abf9ac04\/read_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/35c10e2206f2fdfa56ce5a9fe4450317#file-read_usage-py\" class=\"Link--inTextBlock\">\n          read_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>And the <strong>output <\/strong>looks like this:<\/p>\n\n\n\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> &#8212;Read All&#8212;  <br>{&#8216;_id&#8217;: ObjectId(&#8216;5dbad5a4dde338d6e1cd3ea7&#8217;), &#8216;name&#8217;: &#8216;Rubik&#8217;, &#8216;age&#8217;: 33, &#8216;blog&#8217;: &#8216;rubikscode.net&#8217;}  <br>{&#8216;_id&#8217;: ObjectId(&#8216;5dbc2f46ac3310f0215ba277&#8217;), &#8216;name&#8217;: &#8216;Vanja&#8217;, &#8216;age&#8217;: 29, &#8216;blog&#8217;: &#8216;eventroom.org&#8217;} <br>{&#8216;_id&#8217;: ObjectId(&#8216;5dbc2f46ac3310f0215ba278&#8217;), &#8216;name&#8217;: &#8216;Marko&#8217;, &#8216;age&#8217;: 36, &#8216;blog&#8217;: &#8216;rubikscode.net&#8217;} <br> &#8212;&#8212;&#8212;&#8212;&#8211; <br><br> &#8212;Read Many&#8211;<br>  {&#8216;_id&#8217;: ObjectId(&#8216;5dbad5a4dde338d6e1cd3ea7&#8217;), &#8216;name&#8217;: &#8216;Rubik&#8217;, &#8216;age&#8217;: 33, &#8216;blog&#8217;: &#8216;rubikscode.net&#8217;}  <br>{&#8216;_id&#8217;: ObjectId(&#8216;5dbc2f46ac3310f0215ba278&#8217;), &#8216;name&#8217;: &#8216;Marko&#8217;, &#8216;age&#8217;: 36, &#8216;blog&#8217;: &#8216;rubikscode.net&#8217;} <br>&#8212;&#8212;&#8212;&#8212;&#8211;<br><br>  &#8212; Read &#8212;  <br>{&#8216;_id&#8217;: ObjectId(&#8216;5dbad5a4dde338d6e1cd3ea7&#8217;), &#8216;name&#8217;: &#8216;Rubik&#8217;, &#8216;age&#8217;: 33, &#8216;blog&#8217;: &#8216;rubikscode.net&#8217;}  <br>&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update Operations<\/h3>\n\n\n\n<p><br>We implement two update methods: <em>update<\/em> and <em>increase_age<\/em>. The purpose of the first one is to demonstrate how you can update <strong>single <\/strong>document using conditions. The other function demonstrate how you can <strong>encapsulate <\/strong><em>MongoDB<\/em> update <strong>modifiers <\/strong>within functions and tailor repository for your needs. This is arguably the biggest <strong>advantage <\/strong>of this approach.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205177\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_update_operations-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_update_operations.py content, created by NMZivkovic on 01:32PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_update_operations.py\">\n        <tr>\n          <td id=\"file-repository_update_operations-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class UserRepository(object):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    def __init__(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">        mongoclient = MongoClient(&#39;localhost&#39;, 27017)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">        #mongoclient = MongoClient(&#39;mongodb:\/\/localhost:27017&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">        database = mongoclient.local<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">        self._users = database.user<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">    # Create operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    def insert(self, user):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_one(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    def insert_many(self, users):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_many(users)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">    # Read operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">    def read_all(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">    def read_many(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find(conditions)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC23\" class=\"blob-code blob-code-inner js-file-line\">    def read(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC24\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find_one(conditions)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC25\" class=\"blob-code blob-code-inner js-file-line\">  <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC26\" class=\"blob-code blob-code-inner js-file-line\">    # Update operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC27\" class=\"blob-code blob-code-inner js-file-line\">    def update(self, conditions, new_value):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC28\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.update_one(conditions, new_value)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC29\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L30\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"30\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC30\" class=\"blob-code blob-code-inner js-file-line\">    def increment_age(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_update_operations-py-L31\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"31\"><\/td>\n          <td id=\"file-repository_update_operations-py-LC31\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.update_one(conditions, {&#39;$inc&#39; : {&#39;age&#39; : 1}})<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/455c167609760c6dac5b8e38f50304f6\/raw\/7cfe4b2c0a1c1c65c3b40796e85e4f20b3ac320e\/repository_update_operations.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/455c167609760c6dac5b8e38f50304f6#file-repository_update_operations-py\" class=\"Link--inTextBlock\">\n          repository_update_operations.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>Function <em>update<\/em> can be used to update <em>name<\/em> of a document like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205242\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-update_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"update_usage.py content, created by NMZivkovic on 01:36PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"update_usage.py\">\n        <tr>\n          <td id=\"file-update_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-update_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.update({&#39;name&#39;:&#39;Rubik&#39;}, {&#39;$set&#39;: {&#39;name&#39;: &quot;Nikola&quot;}})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-update_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-update_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/b6675643b707970013b655814c7b33f2\/raw\/ff4de966e4e6aaaa54f5a22c560a5346044e09e7\/update_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/b6675643b707970013b655814c7b33f2#file-update_usage-py\" class=\"Link--inTextBlock\">\n          update_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The <strong>result <\/strong>is this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1007\" height=\"447\" data-attachment-id=\"10349\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage3\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3.jpg\" data-orig-size=\"1007,447\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3.jpg\" alt=\"\" class=\"wp-image-10349\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3.jpg 1007w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3-980x435.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3-480x213.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1007px, 100vw\" \/><figcaption>Name updated<\/figcaption><\/figure>\n\n\n\n<p>Method <em>increment_age<\/em> is even easier to <strong>use<\/strong>:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205303\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-increment_age_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"increment_age_usage.py content, created by NMZivkovic on 01:41PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"increment_age_usage.py\">\n        <tr>\n          <td id=\"file-increment_age_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-increment_age_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.increment_age({&#39;name&#39;:&#39;Nikola&#39;})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-increment_age_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-increment_age_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/77ce25e827244513464edd63d0be9abf\/raw\/ab42487baaa3c31991ee6346eecaa8cf4b1c7c12\/increment_age_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/77ce25e827244513464edd63d0be9abf#file-increment_age_usage-py\" class=\"Link--inTextBlock\">\n          increment_age_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p><strong>Result <\/strong>can be observed in <em>MongoDB Compass<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1007\" height=\"453\" data-attachment-id=\"10351\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage4-1\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1.jpg\" data-orig-size=\"1007,453\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage4-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1.jpg\" alt=\"\" class=\"wp-image-10351\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1.jpg 1007w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1-980x441.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1-480x216.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1007px, 100vw\" \/><figcaption>Age incremented<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Delete Operations<\/h3>\n\n\n\n<p><br>Just like we could create one or multiple documents at once we can delete one or multiple documents. This is done using two <strong>functions<\/strong>: <em>delete<\/em> and <em>delete_many<\/em>. Both of these functions receive conditions for deletion. Here is what the <strong>final form<\/strong> of <em>UserRepository<\/em> class looks like:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205386\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-repository_delete_operations-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"repository_delete_operations.py content, created by NMZivkovic on 01:47PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"repository_delete_operations.py\">\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class UserRepository(object):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    def __init__(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">        mongoclient = MongoClient(&#39;localhost&#39;, 27017)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">        #mongoclient = MongoClient(&#39;mongodb:\/\/localhost:27017&#39;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC6\" class=\"blob-code blob-code-inner js-file-line\">        database = mongoclient.local<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">        self._users = database.user<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC8\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC9\" class=\"blob-code blob-code-inner js-file-line\">    # Create operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC10\" class=\"blob-code blob-code-inner js-file-line\">    def insert(self, user):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC11\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_one(user)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC12\" class=\"blob-code blob-code-inner js-file-line\">        <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC13\" class=\"blob-code blob-code-inner js-file-line\">    def insert_many(self, users):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC14\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.insert_many(users)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC15\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC16\" class=\"blob-code blob-code-inner js-file-line\">    # Read operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC17\" class=\"blob-code blob-code-inner js-file-line\">    def read_all(self):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC18\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find()<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC19\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC20\" class=\"blob-code blob-code-inner js-file-line\">    def read_many(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L21\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"21\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC21\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find(conditions)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L22\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"22\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC22\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L23\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"23\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC23\" class=\"blob-code blob-code-inner js-file-line\">    def read(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L24\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"24\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC24\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.find_one(conditions)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L25\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"25\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC25\" class=\"blob-code blob-code-inner js-file-line\">  <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L26\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"26\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC26\" class=\"blob-code blob-code-inner js-file-line\">    # Update operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L27\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"27\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC27\" class=\"blob-code blob-code-inner js-file-line\">    def update(self, conditions, new_value):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L28\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"28\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC28\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.update_one(conditions, new_value)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L29\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"29\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC29\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L30\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"30\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC30\" class=\"blob-code blob-code-inner js-file-line\">    def increment_age(self, conditions):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L31\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"31\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC31\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.update_one(conditions, {&#39;$inc&#39; : {&#39;age&#39; : 1}})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L32\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"32\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC32\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L33\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"33\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC33\" class=\"blob-code blob-code-inner js-file-line\">    # Delete operations<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L34\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"34\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC34\" class=\"blob-code blob-code-inner js-file-line\">    def delete(self, condition):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L35\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"35\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC35\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.delete_one(condition)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L36\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"36\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC36\" class=\"blob-code blob-code-inner js-file-line\">    <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L37\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"37\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC37\" class=\"blob-code blob-code-inner js-file-line\">    def delete_many(self, condition):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-repository_delete_operations-py-L38\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"38\"><\/td>\n          <td id=\"file-repository_delete_operations-py-LC38\" class=\"blob-code blob-code-inner js-file-line\">        return self._users.delete_many(condition)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/17374c2275ed4df054f11a9627a107b5\/raw\/6c3f3dfd3bc56df10692dbc264c921abf10402f6\/repository_delete_operations.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/17374c2275ed4df054f11a9627a107b5#file-repository_delete_operations-py\" class=\"Link--inTextBlock\">\n          repository_delete_operations.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>If we want to delete <strong>one <\/strong>document, we can do it like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205427\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-delete_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"delete_usage.py content, created by NMZivkovic on 01:50PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"delete_usage.py\">\n        <tr>\n          <td id=\"file-delete_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-delete_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.delete({&#39;name&#39;:&#39;Nikola&#39;})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-delete_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-delete_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/11d3dc1f465fecf92731b869dff74436\/raw\/526b9cbc484caf0c0619e5770864057d2d2c0c27\/delete_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/11d3dc1f465fecf92731b869dff74436#file-delete_usage-py\" class=\"Link--inTextBlock\">\n          delete_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The <strong>result <\/strong>of this operation:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1011\" height=\"348\" data-attachment-id=\"10353\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage5\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5.jpg\" data-orig-size=\"1011,348\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage5\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5.jpg\" alt=\"\" class=\"wp-image-10353\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5.jpg 1011w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5-980x337.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5-480x165.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1011px, 100vw\" \/><figcaption>One document deleted<\/figcaption><\/figure>\n\n\n\n<p>Before we demonstrate what deletion of multiple documents look like, we add document that we just deleted and restore collection in <strong>previous <\/strong>state:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1003\" height=\"447\" data-attachment-id=\"10354\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage6\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6.jpg\" data-orig-size=\"1003,447\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage6\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6.jpg\" alt=\"\" class=\"wp-image-10354\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6.jpg 1003w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6-980x437.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6-480x214.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1003px, 100vw\" \/><\/figure>\n\n\n\n<p>If after this we call <em>delete_many<\/em> that would look like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99205477\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-delete_many_usage-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"delete_many_usage.py content, created by NMZivkovic on 01:54PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"delete_many_usage.py\">\n        <tr>\n          <td id=\"file-delete_many_usage-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-delete_many_usage-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">result = user_repo.delete_many({&#39;blog&#39;:&#39;rubikscode.net&#39;})<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-delete_many_usage-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-delete_many_usage-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">print(result.acknowledged)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/fbca95b8cf8d545d81cc6729b92d26f9\/raw\/91aad4ec4dbd1028148be51adbfd1858ba6df78e\/delete_many_usage.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/fbca95b8cf8d545d81cc6729b92d26f9#file-delete_many_usage-py\" class=\"Link--inTextBlock\">\n          delete_many_usage.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p><strong>Result<\/strong> of this operation:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1007\" height=\"238\" data-attachment-id=\"10355\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/usage7\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7.jpg\" data-orig-size=\"1007,238\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"usage7\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7.jpg\" alt=\"\" class=\"wp-image-10355\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7.jpg 1007w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7-980x232.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7-480x113.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1007px, 100vw\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">MongoEngine Implementation<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"795\" height=\"333\" data-attachment-id=\"10387\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/3-1-6\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/3-1.png\" data-orig-size=\"795,333\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"3-1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/3-1.png\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/3-1.png\" alt=\"\" class=\"wp-image-10387\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/3-1.png 795w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/3-1-480x201.png 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 795px, 100vw\" \/><\/figure>\n\n\n\n<p>As you were able to see, implementing repository using <em>PyMongo<\/em> is quite easy and fun. However, many users don&#8217;t need to steep to this <strong>low-level<\/strong> of abstractions. That is why some people opt to use <strong>library <\/strong>that is built on top of <em>PyMongo<\/em> &#8211; <em>MongoEngine<\/em>. In essence, <em>MongoEngine<\/em> is <strong>ODM<\/strong>, ie. Object Document Mapper. Just like we have <strong>ORMs <\/strong>for relational databases, for document NoSQL databases we have <strong>ODM<\/strong>s. <em>MongoEngine<\/em> provides required <strong>higher-level<\/strong> of abstraction. So, let&#8217;s import it and attach to our database:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99206769\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-import_mongo_engine-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"import_mongo_engine.py content, created by NMZivkovic on 03:07PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"import_mongo_engine.py\">\n        <tr>\n          <td id=\"file-import_mongo_engine-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-import_mongo_engine-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">from mongoengine import * <\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-import_mongo_engine-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-import_mongo_engine-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-import_mongo_engine-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-import_mongo_engine-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">connect(&#39;local&#39;, host=&#39;localhost&#39;, port=27017)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/9fa4d0c25b4975241066bb815f7d4e7c\/raw\/e76ca6cb61d4ad9b1a92a247d4e9c1825982ae8b\/import_mongo_engine.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/9fa4d0c25b4975241066bb815f7d4e7c#file-import_mongo_engine-py\" class=\"Link--inTextBlock\">\n          import_mongo_engine.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The first parameter of the function is the <strong>name <\/strong>of the database, while the other two are defining <strong>location<\/strong>.  In order to &#8220;<em>attach<\/em>&#8221; to collection, we need to implement a <strong>class <\/strong>that describes documents from that collection &#8211; <strong>model<\/strong>. This is very similar to the way we do it in various <em>ORMs<\/em>, so if you have experience with, let&#8217;s say, <em>EntityFramework<\/em> this will come natural to you. The model class has to <strong>inherit <\/strong><em>Document.<\/em> For our <em>user<\/em> collection that looks like this:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99207032\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-user-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"user.py content, created by NMZivkovic on 03:22PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"user.py\">\n        <tr>\n          <td id=\"file-user-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-user-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">class User(Document):<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-user-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-user-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    name = StringField(required=True, max_length=50)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-user-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-user-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">    age = IntField(required=True)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-user-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-user-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    blog = StringField(required=True, max_length=50)<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/4c68578a315c7a7bbc6db0b6a9344095\/raw\/2de46176cd7f4ab960816f28fea1161cf9529e15\/user.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/4c68578a315c7a7bbc6db0b6a9344095#file-user-py\" class=\"Link--inTextBlock\">\n          user.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>In this model, we\u2019ve told <em>MongoEngine <\/em>that we expect <em>User<\/em> to have <em>name, age<\/em> and <em>blog.<\/em> Underneath, <em>Document<\/em> base object <strong>validates <\/strong>data that is provided. <em>MongoEngine<\/em> provides various field <strong>types <\/strong>(here we used just <em>StringField<\/em> and <em>IntField<\/em>) and <strong>options <\/strong>for those types. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Create Operation<\/h3>\n\n\n\n<p><br>When we implemented <em>User<\/em> model class, it is quite easy to <strong>manipulate <\/strong>the database objects with it. Here is how we can create document in <em>user<\/em> collection:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99207126\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-create_user-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"create_user.py content, created by NMZivkovic on 03:28PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"create_user.py\">\n        <tr>\n          <td id=\"file-create_user-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-create_user-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user = User(<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-create_user-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">    name = &quot;Rubik&quot;,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-create_user-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">    age = 33,<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-create_user-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    blog = &quot;rubikscode.net&quot;)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-create_user-py-LC5\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-create_user-py-LC6\" class=\"blob-code blob-code-inner js-file-line\"># Create Operation<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-create_user-py-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-create_user-py-LC7\" class=\"blob-code blob-code-inner js-file-line\">user.save()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/74a85adafc82d1159d8b3ef234bbbc31\/raw\/01d98870911c3d24a968facc115ba7c56d0a36c7\/create_user.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/74a85adafc82d1159d8b3ef234bbbc31#file-create_user-py\" class=\"Link--inTextBlock\">\n          create_user.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>Note that <em>save<\/em> method <strong>creates <\/strong>document in <em>user<\/em> collection in this particular case.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1009\" height=\"241\" data-attachment-id=\"10360\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/me_usage1\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1.jpg\" data-orig-size=\"1009,241\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"me_usage1\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1.jpg\" alt=\"\" class=\"wp-image-10360\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1.jpg 1009w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1-980x234.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1-480x115.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1009px, 100vw\" \/><figcaption>Document created<\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Read Operations<\/h3>\n\n\n\n<p>Reading data is also <strong>simplified<\/strong>. Here is how it is done:<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99207164\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-reading_operation-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"reading_operation.py content, created by NMZivkovic on 03:30PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"reading_operation.py\">\n        <tr>\n          <td id=\"file-reading_operation-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-reading_operation-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">read_users = User.objects<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-reading_operation-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-reading_operation-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-reading_operation-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-reading_operation-py-LC3\" class=\"blob-code blob-code-inner js-file-line\">for user in User.objects:<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-reading_operation-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-reading_operation-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">    print(&quot;Name: {}; Age: {}; Blog: {}&quot;.format(user.name, user.age, user.blog))<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/e5813c4efc486a3dabc0c2922f6f7aab\/raw\/baeef370072ec3b8c55562aa173bf5dfaa2c439a\/reading_operation.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/e5813c4efc486a3dabc0c2922f6f7aab#file-reading_operation-py\" class=\"Link--inTextBlock\">\n          reading_operation.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>Using <em>objects<\/em> field we can access all documents in the database. In this case there is only one:<\/p>\n\n\n\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\">Name: Rubik; Age: 33; Blog: rubikscode.net<\/p>\n\n\n\n<p>We can utilize <em>filter<\/em> function on top of that if we want to create various <strong>queries<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Update Operation<\/h3>\n\n\n\n<p><br>Update is done again using <em>save<\/em> method on <strong>existing <\/strong>object. Basically, when you call <em>save<\/em> method for the first time, it will create a document, while the second time it will <strong>update <\/strong>it.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99207248\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-update_operation-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"update_operation.py content, created by NMZivkovic on 03:36PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"update_operation.py\">\n        <tr>\n          <td id=\"file-update_operation-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-update_operation-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user.name = &quot;Marko&quot;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-update_operation-py-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-update_operation-py-LC2\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-update_operation-py-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-update_operation-py-LC3\" class=\"blob-code blob-code-inner js-file-line\"># Update<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-update_operation-py-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-update_operation-py-LC4\" class=\"blob-code blob-code-inner js-file-line\">user.save()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/dc388eba85a89e4d80297e48b5ba32b9\/raw\/acb64e64f9231e94acc4e7274dd956e5fc51c32c\/update_operation.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/dc388eba85a89e4d80297e48b5ba32b9#file-update_operation-py\" class=\"Link--inTextBlock\">\n          update_operation.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1006\" height=\"233\" data-attachment-id=\"10362\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/me_usage2\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2.jpg\" data-orig-size=\"1006,233\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"me_usage2\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2.jpg\" alt=\"\" class=\"wp-image-10362\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2.jpg 1006w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2-980x227.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2-480x111.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1006px, 100vw\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Delete Operation<\/h3>\n\n\n\n<p><br>Delete operation is performed simply by calling <em>delete<\/em> method on <em>User<\/em> object.<\/p>\n\n\n<p><style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist99207320\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-delete_operation-py\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-python  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"delete_operation.py content, created by NMZivkovic on 03:40PM on November 01, 2019.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"delete_operation.py\">\n        <tr>\n          <td id=\"file-delete_operation-py-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-delete_operation-py-LC1\" class=\"blob-code blob-code-inner js-file-line\">user.delete()<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/eff4eeb1c810e901edc731b2250721da\/raw\/a4f649060945b50a294aa1591f72992164a50ddc\/delete_operation.py\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/NMZivkovic\/eff4eeb1c810e901edc731b2250721da#file-delete_operation-py\" class=\"Link--inTextBlock\">\n          delete_operation.py\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n<\/p>\n\n\n<p>The <strong>result <\/strong>looks like this:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1010\" height=\"269\" data-attachment-id=\"10364\" data-permalink=\"https:\/\/rubikscode.net\/2019\/11\/04\/using-mongodb-in-python\/me_usage3\/\" data-orig-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3.jpg\" data-orig-size=\"1010,269\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"me_usage3\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3.jpg\" src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3.jpg\" alt=\"\" class=\"wp-image-10364\" srcset=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3.jpg 1010w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3-980x261.jpg 980w, https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3-480x128.jpg 480w\" sizes=\"auto, (min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) and (max-width: 980px) 980px, (min-width: 981px) 1010px, 100vw\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we had a chance to see how we can use <em>MongoDB<\/em> in <em>Python<\/em>. We could see that they are pretty <strong>comparable <\/strong>and function well together. We used two modules <em>PyMongo<\/em> and <em>MongoEngine<\/em>, so depending on the level of abstraction you need, you can pick the one that <strong>suits <\/strong>your project the most.<\/p>\n\n\n\n<p>Thank you for reading!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\n\nRead more posts from the author at&nbsp;<strong><a href=\"https:\/\/rubikscode.net\/\">Rubik\u2019s Code<\/a><\/strong>.\n\n<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we implement one repository, using which you can manipulate data in the collection Users. Something similar we have done in .NET in the Definite Guide to MongoDB.<\/p>\n","protected":false},"author":1,"featured_media":10384,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"<!-- wp:paragraph -->\n<p>In our <a rel=\"noreferrer noopener\" aria-label=\"Guide to MongoDB (opens in a new tab)\" href=\"https:\/\/www.educative.io\/collection\/10370001\/5227455105204224?utm_source=sendgrid&amp;utm_medium=email&amp;utm_campaign=mongodb&amp;utm_content=button&amp;utm_source=Newsletter&amp;utm_medium=email&amp;utm_content=The+Definitive+Guide+to+MongoDB&amp;utm_campaign=%5BEU+%2B+AFR%5D+MongoDB\" target=\"_blank\">Definite Guide to MongoDB<\/a>, we covered a lot of ground. There you can get more information about NoSQL databases, what they are, how they can be used, and what the benefits of using one. Apart from that, you can find more information about different types of NoSQL databases as well as their most popular representatives. One of those types is Document NoSQL databases and their most popular representative is <a rel=\"noreferrer noopener\" href=\"https:\/\/www.mongodb.com\/\" target=\"_blank\">MongoDB<\/a>. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>So, we went on and covered some of the foundations of this database. There one can learn how to install MongoDB, create databases, collections, documents and how to use these entities. Also, there is a lot of information about MongoDB deployment, shards, replica sets and how to manipulate them. You can find out how to use this database in .NET environment, in JavaScript MEAN framework and in Serverless environment too. So we wanted to build up on that and write an article that will cover how you can use MongoDB in Python.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>In this article, we implement one repository, using which you can manipulate data in the collection <em>Users<\/em>. Something similar we have done in .NET in the <a rel=\"noreferrer noopener\" href=\"https:\/\/www.educative.io\/collection\/10370001\/5227455105204224?utm_source=sendgrid&amp;utm_medium=email&amp;utm_campaign=mongodb&amp;utm_content=button&amp;utm_source=Newsletter&amp;utm_medium=email&amp;utm_content=The+Definitive+Guide+to+MongoDB&amp;utm_campaign=%5BEU+%2B+AFR%5D+MongoDB\" target=\"_blank\">Definite Guide to MongoDB<\/a>. In fact, we present two ways to achive that, ie. using two <em>Python<\/em> modules - <em>PyMongo <\/em>and <em>MongoEngine<\/em>. The implementation with first library <em>PyMongo<\/em> is low-level implementation, while the implementation with <em>MongoEngine<\/em> can be observed as higher-level implementation. However, before we get into nitti-gritti details of each implementation, let's first install <em>MongoDB<\/em> and both <em>Python<\/em> modules.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>Prerequisites<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p><\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p><em>MongoDB <\/em>is free and open-source. It can be downloaded from&nbsp;<a href=\"https:\/\/www.mongodb.com\/download-center?jmp=nav#community\">here<\/a>, after picking up version and your operating system. In this example, we use <em>MongoDB 4.2.1 Comunity Server<\/em> for <em>Windows<\/em>.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10311,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/i1.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image.png?fit=1024%2C375&amp;ssl=1\" alt=\"\" class=\"wp-image-10311\"\/><figcaption>MongoDB Download Page<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>After installation, a&nbsp;user can run MongoDB server by using the command:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"customTextColor\":\"#22aa56\",\"customBackgroundColor\":\"#424344\",\"fontSize\":\"small\"} -->\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> mongod --dbpath PATH_TO_THE_DIR<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>With this installation, <em>MongoCompass<\/em> is installed as well. That is visual GUI component, using which you can manipulate your databases, collections and documents:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10332,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/i1.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-1.png?fit=1024%2C374&amp;ssl=1\" alt=\"\" class=\"wp-image-10332\"\/><figcaption>MongoDB Compass<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>For the purpose of these examples, we use <em>local<\/em> database and <em>users<\/em> collection:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10334,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/i0.wp.com\/rubikscode.net\/wp-content\/uploads\/2019\/10\/image-2.png?fit=1024%2C351&amp;ssl=1\" alt=\"\" class=\"wp-image-10334\"\/><figcaption>'User' Collection<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>As we mentioned you are going to need two <em>Python<\/em> modules, so let's install them. First you can install <em>PyMongo<\/em> like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"customTextColor\":\"#22aa56\",\"customBackgroundColor\":\"#424344\",\"fontSize\":\"small\"} -->\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> pip install pymongo<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>After that <em>MongoEngine<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"customTextColor\":\"#22aa56\",\"customBackgroundColor\":\"#424344\",\"fontSize\":\"small\"} -->\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\">pip install mongoengine<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>And that is pretty much it. We are ready for implementations.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading -->\n<h2>PyMongo Implementation<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Let's implement <em>UserRepository<\/em> class is using <em>PyMongo <\/em>module. To be more precise, we need just one class from this module - <em>MongoClient<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/277335f96b707a82a00d183916382e66<\/p>\n\n<!-- wp:paragraph -->\n<p>In the constructor of the <em>UserRepository<\/em>, we create instance of <em>MongoClient<\/em>  and connect to the cluster. There are several ways you can achieve that, and you can see two ways in the code. Then you pick database and collection. The rest of the implementation is more less just wrapping functions from <em>self._user<\/em> field.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/28b1ff2c72570653319e59f3cbe75b85<\/p>\n\n<!-- wp:paragraph -->\n<p>Now, we are able to create an object of our class like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/4442256790852affdb0efdd924446405<\/p>\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Create Operations<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Next, we implement <em>create<\/em> operations. We add two methods: <em>insert<\/em> and <em>insert_many<\/em>. First method adds just one <em>user<\/em> to the collection, while the other adds all the users from the array. We can see that underneath they are just utilizing functions <em>insert_one<\/em> and <em>insert_many<\/em> from <em>PyMongo<\/em>.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/8d08ea593b8cd47c856bc4ed58f95d00<\/p>\n\n<!-- wp:paragraph -->\n<p>Now, we can use these functions from our object. Method <em>insert<\/em> can be used like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/58279de6f02f465daf18157b707bfdca<\/p>\n\n<!-- wp:paragraph -->\n<p>When we peek into this collection using <em>MongoDB Compass<\/em> we can see that document is created in <em>user<\/em> collection.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10340,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage1.jpg\" alt=\"\" class=\"wp-image-10340\"\/><figcaption>Created one document<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>While <em>insert_many<\/em> can be used like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/f5b9129a2f1d649dc481635831fa7827<\/p>\n\n<!-- wp:paragraph -->\n<p>The result of this operation once again can be checked visually using <em>MongoDB Compass<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10342,\"align\":\"center\"} -->\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage2.jpg\" alt=\"\" class=\"wp-image-10342\"\/><figcaption>Created multiple documents<\/figcaption><\/figure><\/div>\n<!-- \/wp:image -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Read Operations<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Cool, now when we can add documents into our collection, let's implement functions that are able to retrieve this information. We add three new functions: <em>read_all<\/em>, <em>read_many<\/em> and <em>read<\/em>. Function <em>read_all<\/em> returns all documents from the collection. Methods <em>read<\/em> and <em>read_many<\/em> are similar, they both accept some kind of conditions. The first one will, however, return just one document that satisfies the conditions, while other will return all documents that satisfy the condition.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/2a72602609c7bef69a8a2b78211dc59b<\/p>\n\n<!-- wp:paragraph -->\n<p>These functions can be used like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/35c10e2206f2fdfa56ce5a9fe4450317<\/p>\n\n<!-- wp:paragraph -->\n<p>And the output looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"customTextColor\":\"#22aa56\",\"customBackgroundColor\":\"#424344\",\"fontSize\":\"small\"} -->\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\"> ---Read All---  <br>{'_id': ObjectId('5dbad5a4dde338d6e1cd3ea7'), 'name': 'Rubik', 'age': 33, 'blog': 'rubikscode.net'}  <br>{'_id': ObjectId('5dbc2f46ac3310f0215ba277'), 'name': 'Vanja', 'age': 29, 'blog': 'eventroom.org'} <br>{'_id': ObjectId('5dbc2f46ac3310f0215ba278'), 'name': 'Marko', 'age': 36, 'blog': 'rubikscode.net'} <br> -------------- <br><br> ---Read Many--<br>  {'_id': ObjectId('5dbad5a4dde338d6e1cd3ea7'), 'name': 'Rubik', 'age': 33, 'blog': 'rubikscode.net'}  <br>{'_id': ObjectId('5dbc2f46ac3310f0215ba278'), 'name': 'Marko', 'age': 36, 'blog': 'rubikscode.net'} <br>--------------<br><br>  --- Read ---  <br>{'_id': ObjectId('5dbad5a4dde338d6e1cd3ea7'), 'name': 'Rubik', 'age': 33, 'blog': 'rubikscode.net'}  <br>--------------<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Update Operations<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>We implement two update methods: <em>update<\/em> and <em>increase_age<\/em>. The purpose of the first one is to demonstrate how you can update single document using conditions. The other function demonstrate how you can encapsulate <em>MongoDB<\/em> update modifiers within functions and tailor repository for your needs. This is arguably the biggest advantage of this approach.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/455c167609760c6dac5b8e38f50304f6<\/p>\n\n<!-- wp:paragraph -->\n<p>Function <em>update<\/em> can be used to update <em>name<\/em> of a document like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/b6675643b707970013b655814c7b33f2<\/p>\n\n<!-- wp:paragraph -->\n<p>The result is this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10349} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage3.jpg\" alt=\"\" class=\"wp-image-10349\"\/><figcaption>Name updated<\/figcaption><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Methog <em>increment_age<\/em> is even easier to use:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/77ce25e827244513464edd63d0be9abf<\/p>\n\n<!-- wp:paragraph -->\n<p>Result can be observed in <em>MongoDB Compass<\/em>:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10351} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage4-1.jpg\" alt=\"\" class=\"wp-image-10351\"\/><figcaption>Age incremented<\/figcaption><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Delete Operations<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Just like we could create one or multiple documents at once we can delete one or multiple documents. This is done using two functions: <em>delete<\/em> and <em>delete_many<\/em>. Both of these functions receive conditions for deletion. Here is what the final form of <em>UserRepository<\/em> class looks like:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/17374c2275ed4df054f11a9627a107b5<\/p>\n\n<!-- wp:paragraph -->\n<p>If we want to delete one document, we can do it like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/11d3dc1f465fecf92731b869dff74436<\/p>\n\n<!-- wp:paragraph -->\n<p>The result of this operation:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10353} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage5.jpg\" alt=\"\" class=\"wp-image-10353\"\/><figcaption>One document deleted<\/figcaption><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>Before we demonstrate what deletion of multiple documents look like, we add document that we just deleted and restore collection in previous state:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10354} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage6.jpg\" alt=\"\" class=\"wp-image-10354\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:paragraph -->\n<p>If after this we call <em>delete_many<\/em> that would look like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/fbca95b8cf8d545d81cc6729b92d26f9<\/p>\n\n<!-- wp:paragraph -->\n<p>Result of this operation:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10355} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/usage7.jpg\" alt=\"\" class=\"wp-image-10355\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:heading -->\n<h2>MongoEngine Implementation<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>As you were able to see, implementing repository using <em>PyMongo<\/em> is quite easy and fun. However, many users don't need to steep to this low level of abstractions. That is why some people opt to use library that is built on top of <em>PyMongo<\/em> - <em>MongoEngine<\/em>. In essence, <em>MongoEngine<\/em> is ODM, ie. Object Document Mapper. Just like we have ORMs for relational databases, for document NoSQL databases we have ODM. To sum it up, <em>MongoEngine<\/em> provides higher level of abstraction. So, let's import it and attach to our database:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/9fa4d0c25b4975241066bb815f7d4e7c<\/p>\n\n<!-- wp:paragraph -->\n<p>The first parameter of the function is the name of the database, while the other two are defining location.  In order to \"<em>attach<\/em>\" to collection, we need to implement a class that describes documents from that collection - a model. This is very similar to the way we do it in various ORMs, so if you have experience with, let's say, <em>EntityFramework<\/em> this will come natural to you. The model class has to inherit <em>Document.<\/em> For our <em>user<\/em> collection that looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/4c68578a315c7a7bbc6db0b6a9344095<\/p>\n\n<!-- wp:paragraph -->\n<p>In this model, we\u2019ve told <em>MongoEngine <\/em>that we expect <em>User<\/em> to have <em>name, age<\/em> and <em>blog.<\/em> Underneath, <em>Document<\/em> base object validates data that is provided. <em>MongoEngine<\/em> provides various field types (here we used just <em>StringField<\/em> and <em>IntField<\/em>) and options for those types. <\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Create Operation<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>When we implemented <em>User<\/em> model class, it is quite easy to manipulate the database objects with it. Here is how we can create document in <em>user<\/em> collection:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/74a85adafc82d1159d8b3ef234bbbc31<\/p>\n\n<!-- wp:paragraph -->\n<p>Note that <em>save<\/em> method creates document in <em>user<\/em> collection in this particular case.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10360} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage1.jpg\" alt=\"\" class=\"wp-image-10360\"\/><figcaption>Document created<\/figcaption><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Read Operations<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Reading data is also simplified. Here is how it is done:<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/e5813c4efc486a3dabc0c2922f6f7aab<\/p>\n\n<!-- wp:paragraph -->\n<p>Using <em>objects<\/em> field we can access all documents in the database. In this case there is only one:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph {\"customTextColor\":\"#22aa56\",\"customBackgroundColor\":\"#424344\",\"fontSize\":\"small\"} -->\n<p style=\"background-color:#424344;color:#22aa56\" class=\"has-text-color has-background has-small-font-size\">Name: Rubik; Age: 33; Blog: rubikscode.net<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p> We can utilize <em>filter<\/em> function on top of that if we want to create various queries.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Update Operation<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Update is done again using <em>save<\/em> method on existing object. Basically, when you call <em>save<\/em> method for the first time, it will create a document, while the second time it will update it.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/dc388eba85a89e4d80297e48b5ba32b9<\/p>\n\n<!-- wp:paragraph -->\n<p>The result:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10362} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage2.jpg\" alt=\"\" class=\"wp-image-10362\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:heading {\"level\":3} -->\n<h3>Delete Operation<\/h3>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>Delete operation is performed simply by calling <em>delete<\/em> method on <em>User<\/em> object.<\/p>\n<!-- \/wp:paragraph -->\n\n<p>https:\/\/gist.github.com\/NMZivkovic\/eff4eeb1c810e901edc731b2250721da<\/p>\n\n<!-- wp:paragraph -->\n<p>The result looks like this:<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:image {\"id\":10364} -->\n<figure class=\"wp-block-image\"><img src=\"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/me_usage3.jpg\" alt=\"\" class=\"wp-image-10364\"\/><\/figure>\n<!-- \/wp:image -->\n\n<!-- wp:heading -->\n<h2>Conclusion<\/h2>\n<!-- \/wp:heading -->\n\n<!-- wp:paragraph -->\n<p>In this article, we had a chance to see how we can use <em>MongoDB<\/em> in <em>Python<\/em>. We could see that they are pretty comparable and function well together. We used two modules <em>PyMongo<\/em> and <em>MongoEngine<\/em>, so depending on the level of abstraction you need, you can pick the one that suits your project the most.<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:paragraph -->\n<p>Thank you for reading!<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator\"\/>\n<!-- \/wp:separator -->\n\n<!-- wp:paragraph -->\n<p>\n\nRead more posts from the author at&nbsp;<strong><a href=\"https:\/\/rubikscode.net\/\">Rubik\u2019s Code<\/a><\/strong>.\n\n<\/p>\n<!-- \/wp:paragraph -->\n\n<!-- wp:separator -->\n<hr class=\"wp-block-separator\"\/>\n<!-- \/wp:separator -->","_et_gb_content_width":"","advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[608227671,22319505,832],"tags":[608227772,475,551126,21954763,608227669,608227675,608227748,1821740,5270568,2301],"class_list":["post-10308","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","category-nosql","category-python","tag-data","tag-database","tag-mongo","tag-mongodb","tag-nosql","tag-python","tag-software","tag-software-craft","tag-software-craftsmanship","tag-software-development"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/rubikscode.net\/wp-content\/uploads\/2019\/11\/Add-a-heading-1.png","jetpack_shortlink":"https:\/\/wp.me\/p8G8I0-2Gg","jetpack_likes_enabled":false,"jetpack_sharing_enabled":true,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/10308","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/comments?post=10308"}],"version-history":[{"count":33,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/10308\/revisions"}],"predecessor-version":[{"id":10388,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/posts\/10308\/revisions\/10388"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/media\/10384"}],"wp:attachment":[{"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/media?parent=10308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/categories?post=10308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rubikscode.net\/wp-json\/wp\/v2\/tags?post=10308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}