title: How to get values from a PHP file querying MongoDB in jQuery's getJSON()?
body: <p>I am learning MongoDB and hoped that because data stored in MongoDB databases was in a format very similar to JSON I would be able to query MongoDB directly from jQuery's <code>getJSON()</code> method.</p>
  
  <p>I am learning however that it seems you need MongoDB and a <code>driver</code> to access the database from an application.  So I am going with the PHP driver for the time being.  </p>
  
  <p>I am trying to return the <code>value</code> of the <code>content</code> field:  </p>
  
  <p><strong>MongoDB Document</strong></p>
  
  <pre><code>{
     "_id": ObjectId("34576347347"),
     "content": "here is some content",
     "field2": {
       "subfield1": {
         "0": "sf1v0",
         "1": "sf1v1",
         "2": "sf1v2" 
      },
       "subfield2": "value 2" 
    },
     "field2": "value 3" 
  }
  </code></pre>
  
  <p><strong>PHP</strong></p>
  
  <pre><code>&lt;?php 
  
  $dbhost = 'username:password@127.x.xx.x:27017/';
  $dbname = 'dbname';
  $m = new MongoClient("mongodb://$dbhost");
  $db = $m-&gt;$dbname;
  $collection = $db-&gt;myCollection;
  $query = $collection-&gt;find(array("field2.subfield2" =&gt; "value 2"));
  
  header("Content-type: application/json");
  echo json_encode($query);
  
  ?&gt;
  </code></pre>
  
  <p><strong>jQuery</strong></p>
  
  <pre><code>$.getJSON("http://path/to/mongo.php", {cid: href, format: 'json'}, function(results){  
  $("#my_div").html(results[0].content);
  }
  </code></pre>
  
  <p>I think I need to 'get' a few things:</p>
  
  <ol>
  <li><p>What is being returned from the PHP query:</p>
  
  <pre><code>$query = $collection-&gt;find(array("field2.subfield2" =&gt; "value 2"));
  </code></pre>
  
  <p>I think the MongoDB terminology it that it is returning the <code>cursor</code>, but is that a PHP array, or JSON data or something else?  </p></li>
  <li><p>What is the correct 'call' to make in the <code>getJSON()</code> code to return the required data?</p>
  
  <p>At the moment Firebug is showing me:</p>
  
  <pre><code>TypeError: results[0] is undefined
  </code></pre></li>
  </ol>
  
  <p><strong>Update</strong></p>
  
  <p>I changed <code>getJSON()</code> code to:</p>
  
  <pre><code>$("#my_div").html(results.content);
  </code></pre>
  
  <p>And now I don't get an error, but the following from Firebug:</p>
  
  <blockquote>
    <p>Response tab shows:  {} </p>
    
    <p>JSON tab shows:  There are no properties to show for this object.</p>
  </blockquote>