{"id":27268,"date":"2023-02-09T16:55:32","date_gmt":"2023-02-09T11:25:32","guid":{"rendered":"https:\/\/copyassignment.com\/?p=27268"},"modified":"2023-02-09T16:55:34","modified_gmt":"2023-02-09T11:25:34","slug":"python-crud-operations-in-mongodb","status":"publish","type":"post","link":"https:\/\/copyassignment.com\/python-crud-operations-in-mongodb\/","title":{"rendered":"Python | CRUD operations in MongoDB"},"content":{"rendered":"\n<p>Today in this article, we will talk about some operations that can be performed on the database using the queries of MongoDB. In this article on CRUD operations in MongoDB with Python, we will learn how to perform the operations like Create, Read, Update, and Delete to manipulate the documents in the database. This time we will perform these operations using Python. Before performing those 4 basic operations, let us look at how to use and connect MongoDB with Python.<\/p>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Installing pymongo<\/h2>\n\n\n\n<p>To access the MongoDB database and perform CRUD operations in MongoDB, Python needs a MongoDB driver. We&#8217;ll be using the &#8220;PyMongo&#8221; MongoDB driver in this article. Tools for dealing with the MongoDB database from Python are included in the <a href=\"https:\/\/pypi.org\/project\/pymongo\/\" target=\"_blank\" rel=\"noreferrer noopener\">PyMongo package<\/a>. Python.exe must be present in your system&#8217;s PATH for the installer to succeed. Define Python in your system environment accordingly. Using the pip tool, you can install MongoDB Connector.<\/p>\n\n\n\n<p><strong><em>Enter the following command on the command prompt to install PyMongo:<\/em><\/strong><\/p>\n\n\n\n<p class=\"has-white-color has-black-background-color has-text-color has-background\">pip install pymongo<\/p>\n\n\n\n<p>Now that we have installed pymongo, the next step is to start our MongoDB Database Server<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Starting MongoDB Database Server<\/h2>\n\n\n\n<p>This can simply be done by opening the Terminal and by typing <strong>&#8220;mongo<\/strong>&#8220;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"484\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-1024x484.png\" alt=\"starting mongodb server\" class=\"wp-image-28225 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-1024x484.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-300x142.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-768x363.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-1026x485.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3-675x319.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-3.png 1395w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/484;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Importing pymongo and Establishing Connection <\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>from pymongo import MongoClient\nclient = MongoClient(\"mongodb:\/\/localhost:27017\")<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>Finally, we are starting with the CRUD operations in MongoDB. First, we need to import our installed library. This can be done by the first line written above. Now to establish the connection with MongoDB, we need to enter the <strong>Connection UR<\/strong>I. This URI can be found when you open your MongoDB Compass. This URL needs to be passed to the MongoClient.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"371\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-1024x371.png\" alt=\"CRUD operations in MongoDB with Python\" class=\"wp-image-27386 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-1024x371.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-300x109.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-768x278.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-1536x556.png 1536w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-1026x372.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100-675x245.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-111100.png 1899w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/371;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Connecting to Database and Retrieving Collection<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>database_name = \"Customer\"\ndb = client&#91;database_name]\n\ncollection_name = \"customer_info\"\ncollection = db&#91;collection_name]<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>Now that we have made our connection, it is time to make a database. To make a database we first declared a variable and entered the name of our database into it. Then, we passed that to the client variable that stores our connection. Once the database is connected, now we can access the collections in it or make collections. In the same way, we can make or access the collection by passing it to the variable that stores our database info.<\/p>\n\n\n\n<p><strong>NOTE<\/strong>: It must be kept in mind that we cannot see either the database or collection in MongoDB compass until and unless there is a document in it. So whenever we are creating a database or collection, it is necessary to at least insert one document to check whether our database and collection are created or not.<\/p>\n\n\n\n<p>Finally, we will start with CRUD operations in MongoDB with Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">C &#8211; Creating \/ Inserting documents <\/h2>\n\n\n\n<p>There are two methods for adding documents to a collection:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Adding just one document to the collection.<\/strong><\/li>\n\n\n\n<li><strong>Adding a number of documents to the collection<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Let us take a look at the below code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collection.insert_many(&#91;{\n    \"contact\": 8877665544,\n    \"item_purchased\": \"motorola\",\n    \"name\": \"sam johnson\",\n    \"country\": \"australia\",\n    \"state\": \"new south wales\",\n    \"store\": \"moto store - australia\"\n},\n    {\"contact\": 1234512345,\n     \"item_purchased\": \"iphone\",\n     \"name\": \"borris john\",\n     \"state\": \"texas\",\n     \"store\": \"ivenus - texas\",\n     \"country\": \"america\"\n     },\n    {\n    \"contact\": 3245678412,\n\n    \"item_purchased\": \"micormax\",\n    \"name\": \"harry berklin\",\n    \"country\": \"australia\",\n    \"state\": \"Queensland\",\n    \"store\": \"micro store - australia\"\n},\n    {\n    \"contact\":  9898989898,\n    \"item_purchased\": \"samsung galaxy \",\n    \"name\": \"malella harison\",\n    \"state\": \"karnataka\",\n    \"store\": \"samsung - india\",\n    \"country\": \"India\"\n},\n\n])<\/code><\/pre>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>insert_one()<\/strong> &#8211; This function is used to add a single document to a collection<\/li>\n\n\n\n<li><strong>insert_many() &#8211; <\/strong>This method is used to add numerous documents. In this instance, we&#8217;ve added a number of papers to the &#8220;customer_info&#8221; collection. Here, in the insert_many() function we use commas to divide up numerous items before passing them into the customer_info collection. Multiple items are being handed in when brackets are used between parentheses. A nested method is a common name for such a method.<\/li>\n<\/ul>\n\n\n\n<p><strong>State of Database before inserting 4 documents<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"593\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1024x593.png\" alt=\"database before data\" class=\"wp-image-27430 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1024x593.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-300x174.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-768x445.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1026x594.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-675x391.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958.png 1504w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/593;\" \/><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><strong>State of Database after inserting 4 documents<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"588\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1024x588.png\" alt=\"database after data\" class=\"wp-image-27431 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1024x588.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-300x172.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-768x441.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1026x589.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-675x388.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1.png 1509w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/588;\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">R &#8211; Reading documents <\/h2>\n\n\n\n<p>This mode of operation simply refers to the accessing\/reading of the documents that are stored in the database. R stands for read in CRUD operations in MongoDB or any other database.<\/p>\n\n\n\n<p>These two methods can be used to get data.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Obtaining one document<\/strong><\/li>\n\n\n\n<li><strong>Obtaining several papers<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Using find_one() to read one document<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">from pymongo import MongoClient\nclient = MongoClient(\"mongodb:\/\/localhost:27017\")\n\ndatabase_name = \"Customer\"\ndb = client[database_name]\n\ncollection_name = \"customer_info\"\ncollection = db[collection_name]\n\nsingle_doc = collection.find_one()\nprint(single_doc)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"235\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-1024x235.png\" alt=\"output\" class=\"wp-image-27791 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-1024x235.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-300x69.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-768x176.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-1536x353.png 1536w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-1026x236.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1-675x155.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1.png 1555w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/235;\" \/><\/figure>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p><strong>find_one() <\/strong>method only provides the first document of a collection that meets the specified criteria.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using find_many() to read one document<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">multiple_doc = collection.find()\n \nfor data in multiple_doc:\n    print(data)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"280\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1024x280.png\" alt=\"output\" class=\"wp-image-27790 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1024x280.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-300x82.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-768x210.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1536x419.png 1536w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-1026x280.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image-675x184.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/image.png 1560w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/280;\" \/><\/figure>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>In MongoDB, if we want to access multiple documents then we use the <strong>find_many()<\/strong>&nbsp;function which is used to return the document or documents depending on the supplied criteria.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">U &#8211; Updating documents<\/h2>\n\n\n\n<p>The following two methods can be used to update the documents.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Changing just one document<\/strong><\/li>\n\n\n\n<li><strong>Changing several documents<\/strong><\/li>\n<\/ol>\n\n\n\n<p>Two arguments must be supplied to the function in order to update the documents. A filter parameter makes up the first parameter, while an update parameter makes up the second. The query that matches the revised document is the filter parameter. The update option specifies the changes that should be made to the document.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using update_one() to read one document<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">from pymongo import MongoClient\nclient = MongoClient(\"mongodb:\/\/localhost:27017\")\n\ndatabase_name = \"Customer\"\ndb = client[database_name]\n\ncollection_name = \"customer_info\"\ncollection = db[collection_name]\n\nupdate_single = collection.update_one({\"name\": \"sam johnson\"}, {\"$set\":{ \"contact\": 1212}})\n\nprint(update_single)<\/pre><\/div>\n\n\n\n<p><strong>Output before executing update_one() function:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"388\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-1024x388.png\" alt=\"output before update\" class=\"wp-image-27904 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-1024x388.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-300x114.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-768x291.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-1026x389.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431-675x256.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085431.png 1504w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/388;\" \/><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><strong>Output after executing update_one() function:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"392\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-1024x392.png\" alt=\"output after update\" class=\"wp-image-27906 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-1024x392.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-300x115.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-768x294.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-1026x392.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1-675x258.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-1.png 1501w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/392;\" \/><\/figure>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>We utilize the <strong>update_one()<\/strong> function to update a single document. Two arguments must be supplied to the procedure in order to update the documents. A filter parameter makes up the first parameter, while an update parameter makes up the second. The query that matches the revised document is the filter parameter. The update option specifies the changes that should be made to the document. The value for the fields we want to change will be provided using the &#8220;$set&#8221; key in the following step. The update will be applied if the specified filter matches the already-existing record.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Using update_many() to read many documents<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>update_multiple = collection.update_many({\"country\": \"australia\"}, {\"$set\":{ \"country\": \"AUS\"}})\n\nprint(update_multiple)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"525\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-1024x525.png\" alt=\"output\" class=\"wp-image-27910 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-1024x525.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-300x154.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-768x394.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-1026x526.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2-675x346.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-2.png 1512w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/525;\" \/><\/figure>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>Here in the above query, we used the <strong>update_many()<\/strong> function that helps us in updating multiple documents in one go. Here we selected all the documents that have the country <strong>Australia <\/strong>and then using the <strong>$set<\/strong>, we updated it with the value <strong>AUS<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">D &#8211; Deleting documents<\/h2>\n\n\n\n<p>D stands for delete in CRUD operations in MongoDB or any other database. Moving on to the last operation in our article on CRUD operations in Python MongoDB.  Deleting simply refers to removing the documents from the MongoDB database. The following two methods can be used to delete documents.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Removing a single document<\/strong><\/li>\n\n\n\n<li><strong>Eliminating several documents<\/strong><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Removing a single document using the delete_one() function<\/h3>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"xcode\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">from pymongo import MongoClient\nclient = MongoClient(\"mongodb:\/\/localhost:27017\")\n\ndatabase_name = \"Customer\"\ndb = client[database_name]\n\ncollection_name = \"customer_info\"\ncollection = db[collection_name]\n\ndelete_single = collection.delete_one({ \"contact\": 1212})\nprint(delete_single)<\/pre><\/div>\n\n\n\n<p><strong>Output before deleting a single document<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"588\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1024x588.png\" alt=\"data before delete\" class=\"wp-image-27431 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1024x588.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-300x172.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-768x441.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-1026x589.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1-675x388.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-01-123958-1.png 1509w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/588;\" \/><\/figure>\n\n\n\n<p><strong>Output before deleting multiple documents<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"475\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-1024x475.png\" alt=\"data after delete\" class=\"wp-image-27916 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-1024x475.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-300x139.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-768x356.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-1026x476.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102-675x313.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-095102.png 1513w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/475;\" \/><\/figure>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>Like the update and create operations, the delete operation only affects a single collection. Additionally, deletions of a single document are atomic. The documents you wish to delete from a collection can be defined using filters and criteria when performing delete actions. The criteria and filters function similarly to the filter options in Python&#8217;s read-only CRUD operations in MongoDB. One document can be eliminated from a MongoDB collection using the <strong>delete_one()<\/strong> function. Filters identify the document that will be removed. Only records that match the chosen filter are eliminated.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Removing multiple documents using the delete_many() function<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>delete_multiple = collection.update_many({\"country\": \"australia\"}, {\"$set\":{ \"country\": \"AUS\"}})\n\nprint(delete_single)<\/code><\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"529\" data-src=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-1024x529.png\" alt=\"output\" class=\"wp-image-27919 lazyload\" data-srcset=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-1024x529.png 1024w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-300x155.png 300w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-768x397.png 768w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-1026x530.png 1026w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3-675x349.png 675w, https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/Screenshot-2023-02-03-085259-3.png 1501w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/529;\" \/><\/figure>\n\n\n\n<script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script>\n<ins class=\"adsbygoogle\" style=\"display:block; text-align:center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-9886351916045880\" data-ad-slot=\"2002566052\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<p><strong>Explanation:<\/strong><\/p>\n\n\n\n<p>This function is used to delete multiple documents, based on the condition passed in the function, the value is matched and corresponding documents are deleted. We must always be careful whenever using the <strong>delete_many()<\/strong> function, as multiple documents get deleted. Here 2 documents got deleted as after the update operation we had two docs that had <strong>&#8220;AUS&#8221;<\/strong> as the country name.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Dropping a collection<\/h2>\n\n\n\n<p>To drop an entire collection in one go, we can simply use the <strong>drop()<\/strong> function. Executing this will delete all the documents that are stored in that particular document and the user will not be able to retrieve those documents.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collection.drop()<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Required Files<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<div><a style=\"color:white; background-color:black; font-size:20px; padding:20px\" href=\"https:\/\/copyassignment.com\/wp-content\/uploads\/2023\/02\/customer_info.csv\" class=\"wp-block-file__button wp-element-button\" download aria-describedby=\"wp-block-file--media-9515cf2b-d99f-4c8b-bbce-103590050401\">Download required file here<\/a><\/div>\n\n\n\n<p><\/p>\n\n\n\n<script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-9886351916045880\"\n     crossorigin=\"anonymous\"><\/script>\n<ins class=\"adsbygoogle\"\n     style=\"display:block\"\n     data-ad-format=\"autorelaxed\"\n     data-ad-client=\"ca-pub-9886351916045880\"\n     data-ad-slot=\"7933252109\"><\/ins>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Here is the end of our article on CRUD operations in MongoDB with Python, we learned about some functions that we can use to perform CRUD operations. I tried my best to explain the connection process the easiest way possible. We hope this article turns out to be a great source of learning for you. We will soon be back with another article till then keep Learning, Execting, and Building.<\/p>\n\n\n\n<div style=\"text-align:center\" class=\"wp-block-atomic-blocks-ab-button ab-block-button\"><a href=\"https:\/\/copyassignment.com\/top-100-python-projects-with-source-code\/\" class=\"ab-button ab-button-shape-rounded ab-button-size-medium\" style=\"color:#ffffff;background-color:#3373dc\">Best 100+ Python Projects with source code<\/a><\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong>Also Read:<\/strong><\/p>\n\n\n<ul class=\"wp-block-latest-posts__list is-grid columns-3 wp-block-latest-posts\"><li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/create-your-own-chatgpt-with-python\/\">Create your own ChatGPT with\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sqlite-crud-operations-in-python\/\">SQLite | CRUD Operations in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/event-management-system-project-in-python\/\">Event Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ticket-booking-and-management-in-python\/\">Ticket Booking and Management in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/hostel-management-system-project-in-python\/\">Hostel Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/sales-management-system-project-in-python\/\">Sales Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/bank-management-system-project-in-cpp\/\">Bank Management System Project in C++<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-download-file-from-url-4-methods\/\">Python Download File from URL | 4 Methods<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/python-programming-examples-fundamental-programs-in-python\/\">Python Programming Examples | Fundamental Programs in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/spell-checker-in-python\/\">Spell Checker in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/portfolio-management-system-in-python\/\">Portfolio Management System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/stickman-game-in-python\/\">Stickman Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/contact-book-project-in-python\/\">Contact Book project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/loan-management-system-project-in-python\/\">Loan Management System Project in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/cab-booking-system-in-python\/\">Cab Booking System in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/brick-breaker-game-in-python\/\">Brick Breaker Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tank-game-in-python\/\">Tank game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-piano-in-python\/\">GUI Piano in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/ludo-game-in-python\/\">Ludo Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/rock-paper-scissors-game-in-python\/\">Rock Paper Scissors Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/snake-and-ladder-game-in-python\/\">Snake and Ladder Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/puzzle-game-in-python\/\">Puzzle Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/medical-store-management-system-project-in-python\/\">Medical Store Management System Project in\u00a0Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/creating-dino-game-in-python\/\">Creating Dino Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/tic-tac-toe-game-in-python\/\">Tic Tac Toe Game in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/test-typing-speed-using-python-app\/\">Test Typing Speed using Python App<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python-2\/\">Scientific Calculator in Python<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-to-do-list-app-in-python-tkinter\/\">GUI To-Do List App in Python Tkinter<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/scientific-calculator-in-python\/\">Scientific Calculator in Python using Tkinter<\/a><\/li>\n<li><a class=\"wp-block-latest-posts__post-title\" href=\"https:\/\/copyassignment.com\/gui-chat-application-in-python-tkinter\/\">GUI Chat Application in Python Tkinter<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Today in this article, we will talk about some operations that can be performed on the database using the queries of MongoDB. In this article&#8230;<\/p>\n","protected":false},"author":62,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,1956,1928,1894],"tags":[],"class_list":["post-27268","post","type-post","status-publish","format-standard","hentry","category-allcategorites","category-python","category-python-short-tutorial","category-tutorial","wpcat-22-id","wpcat-1956-id","wpcat-1928-id","wpcat-1894-id"],"_links":{"self":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/27268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/users\/62"}],"replies":[{"embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/comments?post=27268"}],"version-history":[{"count":0,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/posts\/27268\/revisions"}],"wp:attachment":[{"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/media?parent=27268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/categories?post=27268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/copyassignment.com\/wp-json\/wp\/v2\/tags?post=27268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}