

{"id":52470,"date":"2019-03-24T12:27:54","date_gmt":"2019-03-24T06:57:54","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=52470"},"modified":"2021-05-09T13:07:09","modified_gmt":"2021-05-09T07:37:09","slug":"mongodb-php-tutorial","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/","title":{"rendered":"MongoDB PHP Tutorial &#8211; 6 Steps to Connect MongoDB with PHP"},"content":{"rendered":"<p>MongoDB PHP tutorial specially designs to connect MongoDB with PHP. Here, we will see the process with an example for clear understanding. So before wasting time, let&#8217;s discuss it.<\/p>\n<h2>MongoDB PHP<\/h2>\n<p>To use MongoDB PHP driver download it from the following site: <a href=\"\/\/s3.amazonaws.com\/drivers.mongodb.org\/php\/index.html\"><strong>https:\/\/s3.amazonaws.com\/drivers.mongodb.org\/php\/index.html<\/strong><\/a>.<\/p>\n<p>You should download the latest version of it. <strong>Now unzip the archive and put php_mongo.dll in your PHP extension directory.<\/strong><br \/>\n<strong>extension = php_mongo.dll<\/strong><\/p>\n<h2>How to Connect MongoDB with PHP<\/h2>\n<p>Following are the few steps to connect MongoDB PHP:<\/p>\n<h3>1. Make a Connection and Select a Database<\/h3>\n<p>For connecting to the <strong>MongoDB database<\/strong> you need to specify the name of the database, if the database does not exist then MongoDB will create it automatically.<\/p>\n<p>Following is the code for that:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n  \/\/ connect to mongodb\r\n  $m = new MongoClient();\r\n\r\n  echo \"Connection to database successfully\";\r\n  \/\/ select a database\r\n  $db = $m-&gt;examplesdb;\r\n\r\n  echo \"Database examplesdb selected\";\r\n?&gt;<\/pre>\n<p>When you execute the above program, <strong>it will display the following result:<\/strong><\/p>\n<p>Connection to the database successfully<\/p>\n<p>Database examplesdb selected<\/p>\n<h3>2. Create a Collection<\/h3>\n<p>Following is the code for <strong>creating a collection in MongoDB<\/strong>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n  \/\/ connect to mongodb\r\n  $m = new MongoClient();\r\n  echo \"Connection to database successfully\";\r\n\r\n  \/\/ select a database\r\n  $db = $m-&gt;examplesdb;\r\n  echo \"Database examplesdb selected\";\r\n  $collection = $db-&gt;createCollection(\"examplescol\");\r\n  echo \"Collection created succsessfully\";\r\n?&gt;\r\n<\/pre>\n<p><strong>This is the output for the code:<\/strong><\/p>\n<p>Connection to the database successfully<\/p>\n<p>Database examplesdb selected<\/p>\n<p>Collection created successfully<\/p>\n<h3>3. Insert a Document<\/h3>\n<p>insert() method, is used to insert a <strong>document in MongoDB<\/strong>.<\/p>\n<p>Following is the code for inserting a document:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n  \/\/ connect to mongodb\r\n  $m = new MongoClient();\r\n  echo \"Connection to database successfully\";\r\n\r\n  \/\/ select a database\r\n  $db = $m-&gt;examplesdb;\r\n  echo \"Database examplesdb selected\";\r\n  $collection = $db-&gt;examplescol;\r\n  echo \"Collection selected succsessfully\";\r\n\r\n  $document = array( \r\n  \"title\" =&gt; \"MongoDB\", \r\n  \"description\" =&gt; \"database\", \r\n  \"likes\" =&gt; 100,\r\n  \"url\" =&gt; \"http:\/\/www.data-flair.training\/mongodb\/\",\r\n  \"by\" =&gt; \"data flair\"\r\n );\r\n\r\n$collection-&gt;insert($document);\r\necho \"Document inserted successfully\";\r\n?&gt;<\/pre>\n<p><strong>After executing the code you will get the following output:<\/strong><\/p>\n<p>Connection to the database successfully<\/p>\n<p>Database examplesdb selected<\/p>\n<p>Collection selected successfully<\/p>\n<p>Document inserted successfully<\/p>\n<h3>4. Find All Documents<\/h3>\n<p>find() method is used to select all documents from the collection.<\/p>\n<p>Following is the code to find all documents:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n  \/\/ connect to mongodb\r\n  $m = new MongoClient();\r\n  echo \"Connection to database successfully\";\r\n\r\n  \/\/ select a database\r\n  $db = $m-&gt;examplesdb;\r\n  echo \"Database examplesdb selected\";\r\n  $collection = $db-&gt;examplescol;\r\n  echo \"Collection selected succsessfully\";\r\n\r\n  $cursor = $collection-&gt;find();\r\n  \/\/ iterate cursor to display title of documents\r\n\r\n  foreach ($cursor as $document) {\r\n  echo $document[\"title\"] . \"\\n\";\r\n }\r\n?&gt;<\/pre>\n<p><strong>After executing the following code you will get this output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Connection to database successfully\r\nDatabase examplesdb selected\r\nCollection selected succsessfully {\r\n\"title\": \"MongoDB\"\r\n}<\/pre>\n<h3>5. Update a Document<\/h3>\n<p>update() method is used to <strong>update a document in MongoDB<\/strong>.<\/p>\n<p>Following is the code to update a document:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n   \/\/ connect to mongodb\r\n   $m = new MongoClient();\r\n   echo \"Connection to database successfully\";\r\n\r\n   \/\/ select a database\r\n   $db = $m-&gt;examplesdb;\r\n   echo \"Database examplesdb selected\";\r\n   $collection = $db-&gt;examplescol;\r\n   echo \"Collection selected succsessfully\";\r\n\r\n   \/\/ now update the document\r\n   $collection-&gt;update(array(\"name\"=&gt;\"MongoDB\"), \r\n   array('$set'=&gt;array(\"name\"=&gt;\"MongoDB Tutorial\")));\r\n   echo \"Document updated successfully\";\r\n\r\n   \/\/ now display the updated document\r\n   $cursor = $collection-&gt;find();\r\n\r\n   \/\/ iterate cursor to display title of documents\r\n   echo \"Updated document\";\r\n\r\n   foreach ($cursor as $document) {\r\n   echo $document[\"name\"] . \"\\n\";\r\n }\r\n?&gt;<\/pre>\n<p><strong>After executing the program you will get the following output:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Connection to the database successfully\r\nDatabase examplesdb selected\r\nCollection selected successfully\r\nDocument updated successfully\r\nUpdated document {\r\n\"name\": \"MongoDB Tutorial\"\r\n}<\/pre>\n<h3>6. Delete a Document<\/h3>\n<p>remove() method is used to <strong>delete a document in MongoDB<\/strong>.<\/p>\n<p>Following is the code to delete a document:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&lt;?php\r\n   \/\/ connect to mongodb\r\n   $m = new MongoClient();\r\n   echo \"Connection to database successfully\";\r\n\r\n   \/\/ select a database\r\n   $db = $m-&gt;examplesdb;\r\n   echo \"Database examplesdb selected\";\r\n   $collection = $db-&gt;examplescol;\r\n   echo \"Collection selected succsessfully\";\r\n\r\n   \/\/ now remove the document\r\n   $collection-&gt;remove(array(\"name\"=&gt;\"MongoDB Tutorial\"),false);\r\n   echo \"Documents deleted successfully\";\r\n\r\n   \/\/ now display the available documents\r\n   $cursor = $collection-&gt;find();\r\n\r\n   \/\/ iterate cursor to display title of documents\r\n   echo \"Updated document\";\r\n\r\n   foreach ($cursor as $document) {\r\n   echo $document[\"name\"] . \"\\n\";\r\n }\r\n?&gt;<\/pre>\n<p><strong>After executing the program you will get the following output:<\/strong><\/p>\n<p>Connection to the database successfully<\/p>\n<p>Database examplesdb selected<\/p>\n<p>Collection selected successfully<\/p>\n<p>Documents deleted successfully<\/p>\n<h2>Summary<\/h2>\n<p>So, this was all about the MongoDB PHP tutorial, in which we learn 6 steps to connect <strong>MongoDB\u00a0<\/strong>with PHP with examples. Hope, you liked the explanation.<\/p>\n<p>If you have any query or suggestion, post it on the comment box.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB PHP tutorial specially designs to connect MongoDB with PHP. Here, we will see the process with an example for clear understanding. So before wasting time, let&#8217;s discuss it. MongoDB PHP To use MongoDB&#46;&#46;&#46;<\/p>\n","protected":false},"author":6,"featured_media":52519,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[19305,19301,19302,19303],"class_list":["post-52470","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mongodb","tag-connect-mongodb-php","tag-mongodb-for-php","tag-mongodb-php","tag-mongodb-php-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB PHP Tutorial - 6 Steps to Connect MongoDB with PHP - DataFlair<\/title>\n<meta name=\"description\" content=\"This MongoDB PHP tutorial is designed to learn how to connect MongoDB with PHP. Refer the example for more understanding the code and output\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB PHP Tutorial - 6 Steps to Connect MongoDB with PHP - DataFlair\" \/>\n<meta property=\"og:description\" content=\"This MongoDB PHP tutorial is designed to learn how to connect MongoDB with PHP. Refer the example for more understanding the code and output\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-03-24T06:57:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-05-09T07:37:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB PHP Tutorial - 6 Steps to Connect MongoDB with PHP - DataFlair","description":"This MongoDB PHP tutorial is designed to learn how to connect MongoDB with PHP. Refer the example for more understanding the code and output","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"MongoDB PHP Tutorial - 6 Steps to Connect MongoDB with PHP - DataFlair","og_description":"This MongoDB PHP tutorial is designed to learn how to connect MongoDB with PHP. Refer the example for more understanding the code and output","og_url":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2019-03-24T06:57:54+00:00","article_modified_time":"2021-05-09T07:37:09+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89"},"headline":"MongoDB PHP Tutorial &#8211; 6 Steps to Connect MongoDB with PHP","datePublished":"2019-03-24T06:57:54+00:00","dateModified":"2021-05-09T07:37:09+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/"},"wordCount":388,"commentCount":3,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg","keywords":["connect MongoDB Php","MongoDB for PHP","MongoDB PHP","MongoDB PHP Tutorial"],"articleSection":["MongoDB Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/","url":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/","name":"MongoDB PHP Tutorial - 6 Steps to Connect MongoDB with PHP - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg","datePublished":"2019-03-24T06:57:54+00:00","dateModified":"2021-05-09T07:37:09+00:00","description":"This MongoDB PHP tutorial is designed to learn how to connect MongoDB with PHP. Refer the example for more understanding the code and output","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2019\/03\/Steps-to-Connect-MongoDB-with-PHP-01.jpg","width":1200,"height":628,"caption":"MongoDB Java Tutorial"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/mongodb-php-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"MongoDB Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/mongodb\/"},{"@type":"ListItem","position":3,"name":"MongoDB PHP Tutorial &#8211; 6 Steps to Connect MongoDB with PHP"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/2c58ecb4f73a39f0ef993f1ddfcd7b89","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/1ce4a0e3e542444fc73bbebf83e89e8b73e2d95ccb1fcee64da9945f078b97c5?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team provides industry-driven content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our expert educators focus on delivering value-packed, easy-to-follow resources for tech enthusiasts and professionals.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam2\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52470","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=52470"}],"version-history":[{"count":11,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52470\/revisions"}],"predecessor-version":[{"id":93169,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/52470\/revisions\/93169"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/52519"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=52470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=52470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=52470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}