{"id":271,"date":"2012-08-27T01:03:05","date_gmt":"2012-08-26T17:03:05","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=271"},"modified":"2018-09-03T22:35:29","modified_gmt":"2018-09-03T14:35:29","slug":"getting-documents-from-mongodb-collections-with-php","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/getting-documents-from-mongodb-collections-with-php\/","title":{"rendered":"Getting documents from MongoDB collections with PHP"},"content":{"rendered":"<p>We have a MongoDB server <a href=\"http:\/\/www.techcoil.com\/blog\/installing-mongodb-as-a-windows-service\/\" title=\"Installing MongoDB as a windows service\" target=\"_blank\">running as a windows service<\/a> on the same machine as our web server. Over time, this MongoDB server had been listening on port <strong>33333<\/strong> to help us <a href=\"http:\/\/www.techcoil.com\/blog\/inserting-a-document-into-a-collection-in-mongodb-with-php\/\" title=\"Inserting a document into a collection in MongoDB with PHP\" target=\"_blank\">remember information<\/a> that we had collected about our friends. <\/p>\n<p>As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. This post documents some proof of concept that I did for querying documents from MongoDB collection via the <a href=\"http:\/\/www.mongodb.org\/display\/DOCS\/PHP+Language+Center\" title=\"PHP MongoDB driver\" target=\"_blank\">PHP driver<\/a>.<\/p>\n<h3>Information structure of our friends<\/h3>\n<p>While MongoDB can help us remember information about our friends, we need to remember where had we instructed our MongoDB server to keep information about our friends and the document structure for such information. <\/p>\n<p>Those are not hard to remember. We had constantly instructed our MongoDB server to store documents about our friends in the <strong>friend<\/strong> collection within the <strong>contactDb<\/strong> database. These documents are structured in the following format:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$friend = array(\r\n\t'name'         =&gt; '&lt;name&gt;',\r\n\t'birthday'     =&gt; '&lt;dd-mm-yyyy&gt;',\r\n\t'email'        =&gt; '&lt;name@example.com&gt;',\r\n\t'likes'        =&gt; array('&lt;item1&gt;', '&lt;item2&gt;', '&lt;item3&gt;', '...'),\r\n\t'yearBefriended' =&gt; '&lt;yyyy&gt;'\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h4>Existing data in our MongoDB database<\/h4>\n<p>For the sake of this post, further assume that the friend collection in our <strong>contactDb <\/strong>database had accumulated some documents over time. An iteration of the documents via the <code>print_r<\/code> function produces the following output:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; John Jacobs\r\n    &#x5B;email] =&gt; john.jacobs@gmail.com\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<p>Looking at the output, it seems that we do not have much information about John Jacobs. This is fine because MongoDB will not be rigid on the schema of the data that it manages.<\/p>\n<h3>Main steps to query documents from MongoDB with the PHP driver<\/h3>\n<p>Getting documents from collections in MongoDB consists of the following steps:<\/p>\n<ul>\n<li>Derive an instance of <code>MongoCollection<\/code> that represents the MongoDB collection to find documents.<\/li>\n<li>Define a search criteria array.<\/li>\n<li>Define a results filter array.<\/li>\n<li>Execute the find function of the <code>MongoCollection<\/code> instance.<\/li>\n<li>Iterate the results using the <code>MongoCursor<\/code> returned from the <code>find<\/code> function of the <code>MongoCollection<\/code> instance.<\/li>\n<\/ul>\n<h3>Derive an instance of MongoCollection that represents the MongoDB collection to find documents<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n    \r\n$connection = new Mongo('mongodb:\/\/localhost:33333');\r\n$friendCollection = $connection-&gt;selectCollection(&quot;contactDb&quot;, &quot;friend&quot;);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Before we can get anything out of our MongoDB server, we will have to connect to it first. We create an instance of the <a href=\"http:\/\/php.net\/manual\/en\/class.mongo.php\" title=\"PHP Mongo class reference\" target=\"_blank\"><code>Mongo<\/code><\/a> class that connects to our MongoDB server. We then execute the <a href=\"http:\/\/www.php.net\/manual\/en\/mongo.selectcollection.php\" title=\"PHP Mongo selectCollection function reference\" target=\"_blank\"><code>selectionCollection<\/code><\/a> function of the <code>Mongo<\/code> instance to retrieve an instance of the <a href=\"http:\/\/php.net\/manual\/en\/class.mongocollection.php\" title=\"PHP MongoCollection class reference\" target=\"_blank\"><code>MongoCollection<\/code><\/a> class that represents the <code>\"friend\"<\/code> collection in the <code>\"contactDb\"<\/code> database.<\/p>\n<h3>Define a search criteria array<\/h3>\n<p>We define the search criteria via PHP associative arrays. The field names will form the key parts and arrays or literals will form the value parts. For example, to search for Mary Jane's information, we define the following array as the search criteria:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array(\r\n\t'name' =&gt; 'Mary Jane'\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Apart from this simple search criteria, we can also specify <a href=\"#criteriaList\">other types of search criteria<\/a> to get documents from our MongoDB server.<\/p>\n<h3>Define a results filter array<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$filter = array(\t\r\n\t'_id' =&gt; 0,\r\n\t'email' =&gt; 1,\r\n\t'likes' =&gt; 1,\r\n\t'name' =&gt; 1,\r\n\t'yearBefriended' =&gt; 1\r\n\t\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Like the search criteria array, we define the filter array as an associative array.<\/p>\n<p>The filter array will tell MongoDB what key\/value pairs to return for documents that match the search criteria specified. <\/p>\n<p>A non-zero value paired with a key will indicate that we want to see value(s) referenced by the key within the returned documents, where possible.<\/p>\n<p>Note that apart from the _id key having a different visibility state, our MongoDB server currently do not entertain mixed filters. That is we can have this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$filter = array(\t\r\n\t'_id' =&gt; 0,\r\n\t'email' =&gt; 1,\r\n\t'likes' =&gt; 1,\r\n\t'name' =&gt; 1,\r\n\t'yearBefriended' =&gt; 1\r\n\t\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Or this:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$filter = array(\t\r\n\t'_id' =&gt; 1,\r\n\t'email' =&gt; 0,\r\n\t'likes' =&gt; 0,\r\n\t'name' =&gt; 0,\r\n\t'yearBefriended' =&gt; 0\r\n\t\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p><strong>But not this<\/strong>:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$filter = array(\t\r\n\t'_id' =&gt; 0,\r\n\t'email' =&gt; 1,\r\n\t'likes' =&gt; 0,\r\n\t'name' =&gt; 1,\r\n\t'yearBefriended' =&gt; 0\r\n\t\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>In short, we either submit an array of fields to exclude or or an array of fields to include, but not both. <\/p>\n<h3>Execute the find function of the MongoCollection instance<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$resultsCursor = $friendCollection-&gt;find($criteria, $filter);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Once we have our search criteria and filter list, we call the <a href=\"http:\/\/php.net\/manual\/en\/mongocollection.find.php\" title=\"PHP MongoCollection find function reference\" target=\"_blank\"><code>find<\/code><\/a> function of our <code>MongoCollection<\/code> instance (<code>$friendCollection<\/code>) to find the documents that we desire. By doing so, we get an instance of <a href=\"http:\/\/php.net\/manual\/en\/class.mongocursor.php\" title=\"PHP MongoCursor class reference\" target=\"_blank\"><code>MongoCursor<\/code><\/a> which we can use to iterate the results returned by our MongoDB server.<\/p>\n<p>Note that since parameters to the <code>find<\/code> function are optional, we could call the <code>find<\/code> function with either a search criteria array or none at all. <\/p>\n<p>When we call the <code>find<\/code> function with only a search criteria array, we get matching documents with all their fields. <\/p>\n<p>On the other hand, when we call the <code>find<\/code> function without any parameters, we get a full listing of all the documents in the collection with all their fields. <\/p>\n<h3>Iterate the results using the MongoCursor returned from the find function of the MongoCollection instance<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\necho '&lt;pre&gt;';\r\nwhile ($resultsCursor-&gt;hasNext()) { \r\n\t$friend = $cursor-&gt;getNext();\r\n\tprint_r($friend);\r\n        echo PHP_EOL;\r\n}\r\necho '&lt;\/pre&gt;'\r\n\r\n?&gt;\r\n<\/pre>\n<p>We make use of the <a href=\"http:\/\/www.php.net\/manual\/en\/mongocursor.hasnext.php\" title=\"PHP MongoCursor hasNext function reference\" target=\"_blank\"><code>hasNext<\/code><\/a> function and the <a href=\"http:\/\/www.php.net\/manual\/en\/mongocursor.getnext.php\" title=\"PHP MongoCursor getNext function reference\" target=\"_blank\"><code>getNext<\/code><\/a> function of the <a href=\"http:\/\/www.php.net\/manual\/en\/class.mongocursor.php\" title=\"PHP MongoCursor class reference\" target=\"_blank\"><code>MongoCursor<\/code><\/a> instance to iterate through the search results. <\/p>\n<p>The <code>hasNext<\/code> function checks whether there is a document to grab from the results, while the <code>getNext<\/code> function grabs one document from the results and move the cursor to the next document. We put these two functions together with the <code>print_r<\/code> within a while loop to output the contents of documents that matched the search criteria and search filter.<\/p>\n<h3 id='criteriaList'>Some types of search criteria<\/h4>\n<p>The following section is a listing of MongoDB query facilities that I had tried out using the PHP driver. For the sake of brevity, I list only the search criteria arrays and the corresponding results. The results output is produced via the same set of codes as discussed in the earlier sections.   <\/p>\n<h4>Search criteria to find documents with a field value that is greater than (or equals to), less than (or equals to), not equals to another value ($gt, $lt, $gte, $lte, $ne)<\/h4>\n<h5>For finding friends whom we befriended in a year other than 2003, via $ne<\/p>\n<h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'yearBefriended' =&gt; array(\r\n\t\t'$ne' =&gt; 2003\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends whom we befriended in a year other than 2003<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; John Jacobs\r\n    &#x5B;email] =&gt; john.jacobs@gmail.com\r\n)\r\n<\/pre>\n<p>Note that John Jacobs was listed because of the absence of a <code>yearBefriended<\/code> field. <\/p>\n<hr\/>\n<h5>For finding friends whom we befriended in 2003<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'yearBefriended' =&gt; 2003\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friend whom we befriended in 2003<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<hr\/>\n<h5>For finding friends whom we befriended from 2001 to 2003, via $gt and $lt<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'yearBefriended' =&gt; array (\r\n\t\t'$gt' =&gt; 2000,\r\n\t\t'$lt' =&gt; 2004\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends whom we befriended in 2001, 2002 or 2003<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<hr\/>\n<h5>For finding friends whom we befriended in 2000, 2001, 2002, 2003 or 2004, via $gte and $lte<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'yearBefriended' =&gt; array (\r\n\t\t'$gte' =&gt; 2000,\r\n\t\t'$lte' =&gt; 2004\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends whom we befriended in 2000, 2001, 2002, 2003 or 2004<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<h4>Search criteria to find documents that contain or not contain a certain field ($exists)<\/h4>\n<h5>For finding friends who told us what they like<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'likes' =&gt; array('$exists' =&gt; true)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends who told us what they like<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<hr\/>\n<h5>For finding friends who did not tell us what they like<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'likes' =&gt; array('$exists' =&gt; false)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friend who did not tell us what he likes<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; John Jacobs\r\n    &#x5B;email] =&gt; john.jacobs@gmail.com\r\n)\r\n<\/pre>\n<h4>Search criteria to find documents with field of type array having \/ not having one of a list of items ($in, $nin)<\/h4>\n<h5>For finding friends who likes swimming, via $in<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'likes' =&gt; array(\r\n\t\t'$in' =&gt; array(\r\n\t\t\t'Swimming'\r\n\t\t)\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends who likes swimming<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n\r\n<\/pre>\n<hr\/>\n<h5>For finding friends who do not tell us that they like to swim, via $nin<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'likes' =&gt; array (\r\n\t\t'$nin' =&gt; array (\r\n\t\t\t'Swimming'\r\n\t\t)\r\n\t)\r\n);  \r\n\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends who do not say that they like to swim<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; John Jacobs\r\n    &#x5B;email] =&gt; john.jacobs@gmail.com\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<h4>Search criteria to find documents with field of type array having all the items in a list ($all)<\/h4>\n<h5>For finding friends who like both swimming and cooking<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'likes' =&gt; array (\r\n\t\t'$all' =&gt; array (\r\n\t\t\t'Cooking',\r\n\t\t\t'Swimming'\r\n\t\t)\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friend who likes both swimming and cooking<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n<\/pre>\n<h4>Search criteria to find documents with field matching a regular expression<\/h4>\n<h5>For finding friends having \"Jane\" as his\/her surname<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array(\r\n\t'name' =&gt; array (\r\n\t\t'$regex' =&gt; '.*Jane'\r\n\t)\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Or<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'name' =&gt; new MongoRegex('\/^.*Jane\/')\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends having \"Jane\" as his\/her surname<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Mary Jane\r\n    &#x5B;email] =&gt; mary.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Reading\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Baking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2004\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n<\/pre>\n<h4>Search criteria to match documents with any of the stated conditions ($or)<\/h4>\n<h5>For finding friends whom we befriended in year 2000 or year 2003<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'$or' =&gt; array (\r\n\t\tarray (\r\n\t\t\t'yearBefriended' =&gt; 2000\r\n\t\t),\r\n\t\tarray (\r\n\t\t\t'yearBefriended' =&gt; 2003\r\n\t\t)\r\n\t) \r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>Friends whom we befriended in year 2000 or year 2003<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Johnson Peter\r\n    &#x5B;email] =&gt; johnson.peter@outlook.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Jogging\r\n            &#x5B;1] =&gt; Programming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2000\r\n)\r\n\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Michael Jackson\r\n    &#x5B;email] =&gt; michael@singers.net\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Singing\r\n            &#x5B;1] =&gt; Dancing\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2003\r\n)\r\n<\/pre>\n<h4>Search criteria to match documents with all of the stated conditions ($and)<\/h4>\n<h5>For finding the Jane whom we befriended in 2002<\/h5>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t\r\n\t'$and' =&gt; array (\r\n\t\tarray (\r\n\t\t\t'name' =&gt; new MongoRegex('\/^.*Jane\/')\r\n\t\t),\r\n\t\tarray (\r\n\t\t\t'yearBefriended' =&gt; 2002\r\n\t\t)\r\n\t)\r\n\r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<p>Or<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\r\n$criteria = array (\r\n\t'name' =&gt; new MongoRegex('\/^.*Jane\/'),\r\n\t'yearBefriended' =&gt; 2002 \r\n);\r\n\r\n?&gt;\r\n<\/pre>\n<h5>The Jane whom we befriended in 2002<\/h5>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nArray\r\n(\r\n    &#x5B;name] =&gt; Lily Jane\r\n    &#x5B;email] =&gt; lily.jane@gmail.com\r\n    &#x5B;likes] =&gt; Array\r\n        (\r\n            &#x5B;0] =&gt; Diving\r\n            &#x5B;1] =&gt; Shopping\r\n            &#x5B;2] =&gt; Cooking\r\n            &#x5B;3] =&gt; Swimming\r\n        )\r\n\r\n    &#x5B;yearBefriended] =&gt; 2002\r\n)\r\n<\/pre>\n<h3>Related posts<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/my-first-look-at-mongodb-why-i-will-want-to-use-mongodb-in-my-next-project\/\" title=\"My first look at MongoDB \u2013 Why I will want to use MongoDB in my next project\" target=\"_blank\">My first look at MongoDB \u2013 Why I will want to use MongoDB in my next project<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/installing-mongodb-as-a-windows-service\/\" title=\"Installing MongoDB as a windows service\" target=\"_blank\">Installing MongoDB as a windows service<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/mongodb-at-a-conceptual-level\/\" title=\"MongoDB at a conceptual level\" target=\"_blank\">MongoDB at a conceptual level<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/connecting-to-and-disconnecting-from-a-mongodb-server-in-php\/\" title=\"Connecting to and disconnecting from a MongoDB server in PHP\" target=\"_blank\">Connecting to and disconnecting from a MongoDB server in PHP<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/inserting-a-document-into-a-collection-in-mongodb-with-php\/\" title=\"Inserting a document into a collection in MongoDB with PHP\" target=\"_blank\">Inserting a document into a collection in MongoDB with PHP<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-4n\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4n&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-4n&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F271&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>We have a MongoDB server <a href=\"http:\/\/www.techcoil.com\/blog\/installing-mongodb-as-a-windows-service\/\" title=\"Installing MongoDB as a windows service\" target=\"_blank\">running as a windows service<\/a> on the same machine as our web server. Over time, this MongoDB server had been listening on port <strong>33333<\/strong> to help us <a href=\"http:\/\/www.techcoil.com\/blog\/inserting-a-document-into-a-collection-in-mongodb-with-php\/\" title=\"Inserting a document into a collection in MongoDB with PHP\" target=\"_blank\">remember information<\/a> that we had collected about our friends. <\/p>\n<p>As time passes, our memory can hardly rival that of our MongoDB server, which was designed to help us recollect information efficiently. This post documents some proof of concept that I did for querying documents from MongoDB collection via the <a href=\"http:\/\/www.mongodb.org\/display\/DOCS\/PHP+Language+Center\" title=\"PHP MongoDB driver\" target=\"_blank\">PHP driver<\/a>.<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[156,152,153,13],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/mongoDB-logo.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-4n","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/271"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=271"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/271\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}