{"id":1956,"date":"2016-03-22T11:05:17","date_gmt":"2016-03-22T11:05:17","guid":{"rendered":"https:\/\/www.bronco.co.uk\/our-ideas\/?p=1956"},"modified":"2016-03-22T11:08:13","modified_gmt":"2016-03-22T11:08:13","slug":"working-with-mongodb-in-php","status":"publish","type":"post","link":"https:\/\/www.bronco.co.uk\/our-ideas\/working-with-mongodb-in-php\/","title":{"rendered":"Working with MongoDB in PHP"},"content":{"rendered":"<p>If you&#8217;re used to MySQL then making the move to MongoDB may be daunting. Here&#8217;s a simple walkthrough with examples to get you started.<\/p>\n<h2>Setup<\/h2>\n<p>Firstly, connect to your server and select the database with which you wish to work.<\/p>\n<p><code>$m = new MongoClient(\"server.co.uk\");<br \/>\n$db = $m-&gt;selectDB(\"database\");<\/code><\/p>\n<p>Access the relevant collection (equivalent to a table in MySQL). In this example I will be using &#8220;cars&#8221;. If a collection doesn&#8217;t already exist, it will be created.<br \/>\n<code>$collection = $db-&gt;cars;<\/code><\/p>\n<h2>Insert Data<\/h2>\n<p>Inserting data (called a document) into a collection is pretty straight forward.<br \/>\nCreate an array containing the data you want to add:<br \/>\n<code>$insert = array('brand'=&gt;'Ferrari', 'model'=&gt;F40')));<\/code><\/p>\n<p>Then use the &#8220;insert&#8221; command:<br \/>\n<code>$collection-&gt;insert($insert);<\/code><\/p>\n<p>Simple. You can also store arrays within the document:<br \/>\n<code>$insert = array('brand'=&gt;'Ferrari', 'model'=&gt;F40', 'wheels'=&gt;array(['number'=&gt;4, 'size'=&gt;18]));<\/code><\/p>\n<p>If the square brackets are confusing, it&#8217;s the same as this:<br \/>\n<code>$insert = array('brand'=&gt;'Ferrari', 'model'=&gt;F40', 'wheels'=&gt;array(array('number'=&gt;4, 'size'=&gt;18)));<\/code><\/p>\n<h2>Update Data<\/h2>\n<p><code>$criteria = array('brand'=&gt;'Ferrari', 'model'=&gt;'F40');<br \/>\n$new_data = array('$set'=&gt;array('wheels'=&gt;array('size'=&gt;17)));<br \/>\n$collection-&gt;update($criteria, $new_data);<\/code><br \/>\nThe above will find any Ferrari F40 and update the wheel size to 17.<br \/>\n<strong>Note:<\/strong> the dollar ($) doesn&#8217;t denote a variable, and must be enclosed in single quotes.<\/p>\n<p>You can also add &#8220;fields&#8221; to collections in the same way. For example if I wanted to add &#8220;top speed&#8221; to just the Ferrari F40:<br \/>\n<code>$criteria = array('brand'=&gt;'Ferrari', 'model'=&gt;'F40');<br \/>\n$new_data = array('$set'=&gt;array('top speed'=&gt;201));<br \/>\n$collection-&gt;update($criteria, $new_data);<\/code><\/p>\n<h2>Select Data<\/h2>\n<p>If you want to find all Ferraris:<br \/>\n<code>$search = array('brand'=&gt;'Ferrari');<br \/>\n$cars = $collection-&gt;find($search);<\/code><\/p>\n<p>If you want to find all cars with 18&#8243; wheels:<br \/>\n<code>$search = array('wheels.size'=&gt;18);<br \/>\n$cars = $collection-&gt;find($search);<\/code><\/p>\n<p>If you want to find all Ferraris with 18&#8243; wheels:<br \/>\n<code>$search = array('brand'=&gt;'Ferrari', 'wheels.size'=&gt;18);<br \/>\n$cars = $collection-&gt;find($search);<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re used to MySQL then making the move to MongoDB may be daunting. Here&#8217;s a simple walkthrough with examples to get you started. Setup Firstly, connect to your server and select the database with which you wish to work. $m = new MongoClient(&#8220;server.co.uk&#8221;); $db = $m-&gt;selectDB(&#8220;database&#8221;); Access the relevant collection (equivalent to a table [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"class_list":["post-1956","post","type-post","status-publish","format-standard","hentry","category-web-and-ux"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/1956","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/comments?post=1956"}],"version-history":[{"count":0,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/posts\/1956\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/media?parent=1956"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bronco.co.uk\/our-ideas\/wp-json\/wp\/v2\/categories?post=1956"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}