{"id":2857,"date":"2024-03-04T20:41:02","date_gmt":"2024-03-04T15:41:02","guid":{"rendered":"https:\/\/afzalbadshah.com\/?p=2857"},"modified":"2024-03-20T10:00:14","modified_gmt":"2024-03-20T05:00:14","slug":"document-insertion-to-mongodb","status":"publish","type":"post","link":"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/04\/document-insertion-to-mongodb\/","title":{"rendered":"Document Insertion to MongoDB"},"content":{"rendered":"\n<p>In MongoDB, efficient document insertion is key to maintaining a well-organized and responsive database. Whether inserting a single document or multiple documents at once, MongoDB offers straightforward methods for seamlessly integrating data into your collections.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting a Single Document into The MongoDB<\/h2>\n\n\n\n<p>In MongoDB, inserting a single document involves creating a dictionary representing the data to be inserted and using the <code>insert_one()<\/code> method to insert it into the desired collection. For example, if we want to insert a single student document with attributes such as ID and name into a collection named &#8220;students&#8221;, we define the document as a dictionary, specify the collection, and call <code>insert_one()<\/code> to insert it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\n\n# Establish a connection to the MongoDB server\nclient = pymongo.MongoClient(\"mongodb+srv:\/\/badshah:password@cluster.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")\n\n# Select the database and collection\ndb = client.afzal\ncollection = db.afzal\n\n# Define a dictionary representing a student document\nstd = {\"id\": \"123\", \"name\": \"afzal\"}\n\n# Insert the document into the collection\ncollection.insert_one(std)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p><strong>Import MongoDB modules<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient<\/code><\/pre>\n\n\n\n<p>These lines import the necessary modules from pymongo library for interacting with MongoDB.<\/p>\n\n\n\n<p><strong>Establish a connection to the MongoDB server<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client = pymongo.MongoClient(\"mongodb+srv:\/\/badshah:password@cluster.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")<\/code><\/pre>\n\n\n\n<p>This line establishes a connection to the MongoDB server hosted at the specified URI. Replace the URI with your own MongoDB server URI.<\/p>\n\n\n\n<p><strong>Select database and collection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db = client.afzal\ncollection = db.afzal<\/code><\/pre>\n\n\n\n<p>This code selects the database named &#8220;afzal&#8221; and the collection named &#8220;afzal&#8221; within that database. Replace &#8220;afzal&#8221; with your preferred database and collection names.<\/p>\n\n\n\n<p><strong>Define student document<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std = {\"id\": \"123\", \"name\": \"afzal\"}<\/code><\/pre>\n\n\n\n<p>This line defines a dictionary representing a student document to be inserted into the collection. It contains key-value pairs for student ID and name.<\/p>\n\n\n\n<p><strong>Insert a document into the collection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collection.insert_one(std)<\/code><\/pre>\n\n\n\n<p>This line inserts the student document into the selected collection. The <code>insert_one()<\/code> method inserts a single document into the collection.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Inserting Multiple Documents into The MongoDB<\/h2>\n\n\n\n<p>In MongoDB, inserting multiple documents is done by creating a list of dictionaries, each representing a document, and using the <code>insert_many()<\/code> method to insert all the documents at once into the collection. This method is efficient for bulk data insertion. For instance, if we have a list of student documents to be inserted into the &#8220;students&#8221; collection, we create the list of dictionaries, specify the collection, and call <code>insert_many()<\/code> to insert all documents simultaneously.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient\n\n# Establish a connection to the MongoDB server\nclient = pymongo.MongoClient(\"mongodb+srv:\/\/badshah:password@cluster.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")\n\n# Select the database and collection\ndb = client.afzal\ncollection = db.afzal\n\n# Define a list of dictionaries representing student documents\nstd = &#91;\n  {\"id\": \"123\", \"name\": \"afzal\"},\n  {\"id\": \"124\", \"name\": \"afzal\"},\n  {\"id\": \"125\", \"name\": \"afzal\"},\n  {\"id\": \"126\", \"name\": \"afzal\"}\n]\n\n# Insert multiple documents into the collection\ncollection.insert_many(std)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p><strong>Import MongoDB modules<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import pymongo\nfrom pymongo import MongoClient<\/code><\/pre>\n\n\n\n<p>These lines import the necessary modules from pymongo library for interacting with MongoDB.<\/p>\n\n\n\n<p><strong>Establish a connection to the MongoDB server<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>client = pymongo.MongoClient(\"mongodb+srv:\/\/badshah:password@cluster.ergtejf.mongodb.net\/?retryWrites=true&amp;w=majority&amp;appName=Cluster0\")<\/code><\/pre>\n\n\n\n<p>This line establishes a connection to the MongoDB server hosted at the specified URI. Replace the URI with your own MongoDB server URI.<\/p>\n\n\n\n<p><strong>Select database and collection<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>db = client.afzal\ncollection = db.afzal<\/code><\/pre>\n\n\n\n<p>This code selects the database named &#8220;afzal&#8221; and the collection named &#8220;afzal&#8221; within that database. Replace &#8220;afzal&#8221; with your preferred database and collection names.<\/p>\n\n\n\n<p><strong>Define a list of student documents:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>std = &#91;\n  {\"id\": \"123\", \"name\": \"afzal\"},\n  {\"id\": \"124\", \"name\": \"afzal\"},\n  {\"id\": \"125\", \"name\": \"afzal\"},\n  {\"id\": \"126\", \"name\": \"afzal\"}\n]<\/code><\/pre>\n\n\n\n<p>This line defines a list of dictionaries, with each dictionary representing a student document to be inserted into the collection. Each dictionary contains key-value pairs for student ID and name.<\/p>\n\n\n\n<p><strong>Insert multiple documents into the collection:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>collection.insert_many(std)<\/code><\/pre>\n\n\n\n<p>This line inserts the list of student documents into the selected collection using the <code>insert_many()<\/code> method, which inserts multiple documents into the collection at once.<\/p>\n\n\n\n<p><a href=\"https:\/\/afzalbadshah.com\/index.php\/category\/courses\/data-driven-applications-using-mongodb-python-and-google-colab\/\" target=\"_blank\" rel=\"noreferrer noopener\">You can access the detailed tutorial here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In MongoDB, efficient document insertion is key to maintaining a well-organized and responsive database. Whether inserting a single document or multiple documents at once, MongoDB offers straightforward methods for seamlessly integrating data into your collections. Inserting a Single Document into The MongoDB In MongoDB, inserting a single document involves creating a dictionary representing the data to be inserted and using the insert_one() method to insert it into the desired collection. For example, if we want to insert a single student&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/afzalbadshah.com\/index.php\/2024\/03\/04\/document-insertion-to-mongodb\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":2861,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[489],"tags":[523,522],"class_list":["post-2857","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-driven-applications-using-mongodb-python-and-google-colab","tag-inseartion","tag-mongodb"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/afzalbadshah.com\/wp-content\/uploads\/2024\/03\/Slide2.jpg?fit=1280%2C720&ssl=1","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pf3emP-K5","jetpack-related-posts":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/comments?post=2857"}],"version-history":[{"count":6,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2857\/revisions"}],"predecessor-version":[{"id":2951,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/posts\/2857\/revisions\/2951"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media\/2861"}],"wp:attachment":[{"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/media?parent=2857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/categories?post=2857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afzalbadshah.com\/index.php\/wp-json\/wp\/v2\/tags?post=2857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}