{"id":541,"date":"2013-10-18T22:42:28","date_gmt":"2013-10-18T20:42:28","guid":{"rendered":"http:\/\/www.extradrm.com\/?p=541"},"modified":"2013-10-18T22:56:21","modified_gmt":"2013-10-18T20:56:21","slug":"mongodb-tutorial-sample-application","status":"publish","type":"post","link":"https:\/\/www.extradrm.com\/?p=541","title":{"rendered":"MongoDB Tutorial sample PHP application"},"content":{"rendered":"<p>On a wampserver, Install the\u00a0<a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/php_mongo-1.3.1.zip\">php_mongo-1.3.1<\/a> extension and you will be able to connect to mongo. you can refer here to <a title=\"Mongo install\" href=\"http:\/\/docs.mongodb.org\/manual\/tutorial\/install-mongodb-on-windows\/\">mongo installation<\/a>.<\/p>\n<p>You must verify after restarting apache that mongo is activated and of course look Compiler Version for dlls (VC9 or VC6) if VC9 you must use this dll : <a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/mongo-latest-php5.3vc9ts.zip\">mongo-latest-php5.3vc9ts<\/a><\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo001.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1575\" alt=\"mongo001\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo001.jpg\" width=\"621\" height=\"394\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo001.jpg 621w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo001-300x190.jpg 300w\" sizes=\"(max-width: 621px) 100vw, 621px\" \/><\/a><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo002.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1576\" alt=\"mongo002\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo002.jpg\" width=\"615\" height=\"185\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo002.jpg 615w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/01\/mongo002-300x90.jpg 300w\" sizes=\"(max-width: 615px) 100vw, 615px\" \/><\/a><\/p>\n<p>A sample php script to test with this dll is below<\/p>\n<pre>&lt;?php\r\ntry {\r\n\/\/ connect\r\n$m = new MongoClient();\r\n\r\n\/\/ select a database\r\n$db = $m-&gt;comedy;\r\n\r\n\/\/ select a collection (analogous to a relational database's table)\r\n$collection = $db-&gt;cartoons;\r\n\r\n\/\/ add a record\r\n$document = array( \"title\" =&gt; \"Calvin and Hobbes\", \"author\" =&gt; \"Bill Watterson\" );\r\n$collection-&gt;insert($document);\r\n\r\n\/\/ add another record, with a different \"shape\"\r\n$document = array( \"title\" =&gt; \"XKCD\", \"online\" =&gt; true );\r\n$collection-&gt;insert($document);\r\n\r\n\/\/ find everything in the collection\r\n$cursor = $collection-&gt;find();\r\n\r\n\/\/ iterate through the results\r\nforeach ($cursor as $document) {\r\necho $document[\"title\"] . \"\\n\";\r\n}\r\n\/\/ disconnect from server\r\n$m-&gt;close();\r\n} catch (MongoConnectionException $e) {\r\ndie('Error connecting to MongoDB server');\r\n} catch (MongoException $e) {\r\ndie('Error: ' . $e-&gt;getMessage());\r\n}\r\n?&gt;<\/pre>\n<p>THE OUTPUT WILL BE :<\/p>\n<p><a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/mongo003.jpg\"><img loading=\"lazy\" class=\"aligncenter size-full wp-image-1581\" alt=\"mongo003\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/mongo003.jpg\" width=\"354\" height=\"73\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/mongo003.jpg 354w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/05\/mongo003-300x61.jpg 300w\" sizes=\"(max-width: 354px) 100vw, 354px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Another sample will be this script :<\/strong><\/p>\n<pre>\r\n<?php\r\n\r\ntry {\r\n\/\/ Connexion au serveur\r\n$connection = new Mongo();\r\n\r\n\/\/ S\u00e9lection d'une base de donn\u00e9e. Si la base n'existe pas, elle est cr\u00e9\u00e9e\r\n$db = $connection->selectDB('myDb');\r\n\r\n\/\/ S\u00e9lection d'une collection. Si elle n'existe pas, elle est cr\u00e9\u00e9e\r\n$people = $db->selectCollection('people');\r\n\r\n\/\/ On ins\u00e8re un premier enregistrement\r\n$people->insert(\r\n    array(\r\n        'firstname' => 'John',\r\n        'lastname' => 'Doe',\r\n        'email' => 'john.doe@somewhere.com'\r\n    )\r\n);\r\n\r\n\/\/ On ins\u00e8re un second enregistrement. Notez que les colonnes sont diff\u00e9rentes.\r\n$people->insert(\r\n    array(\r\n        'firstname' => 'Jane',\r\n        'lastname' => 'Doe',\r\n        'mobile' => '06.01.02.03.04'\r\n    )\r\n);\r\n\r\n\r\n\r\n\/\/ On retrouve et on affiche tous les \u00e9l\u00e9ments de la collection\r\n$docs = $people->find();\r\nforeach($docs as $doc) {\r\n    var_dump($doc);\r\n    echo '<br\/>';\r\n}\r\n\r\n\/\/ On retrouve Jane par son pr\u00e9nom\r\n$jane = $people->findOne(array('firstname' => 'Jane'));\r\nvar_dump($jane);\r\necho '<br\/>';\r\n\r\n\/\/ disconnect from server\r\n  $connection->close();\r\n} catch (MongoConnectionException $e) {\r\n  die('Error connecting to MongoDB server');\r\n} catch (MongoException $e) {\r\n  die('Error: ' . $e->getMessage());\r\n}\r\n?>\r\n<\/pre>\n<p>THE OUTPUT WILL BE :<br \/>\n<a href=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/mongo004.jpg\"><img loading=\"lazy\" src=\"http:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/mongo004.jpg\" alt=\"mongo004\" width=\"619\" height=\"486\" class=\"aligncenter size-full wp-image-1586\" srcset=\"https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/mongo004.jpg 619w, https:\/\/www.extradrm.com\/wp-content\/uploads\/2013\/10\/mongo004-300x235.jpg 300w\" sizes=\"(max-width: 619px) 100vw, 619px\" \/><\/a><\/p>\n<p>Cheers,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On a wampserver, Install the\u00a0php_mongo-1.3.1 extension and you will be able to connect to mongo. you can refer here to mongo installation. You must verify after restarting apache that mongo is activated and of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":2845,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17,16],"tags":[351,86],"youtube_video":null,"_links":{"self":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/541"}],"collection":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=541"}],"version-history":[{"count":0,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/posts\/541\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=\/wp\/v2\/media\/2845"}],"wp:attachment":[{"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.extradrm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}