Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>{'parent_id': 1} in the find query is saying that you only want to return the key parent_id, think of it like ( and it can be used like ) {'parent_id': true} try it out in the mongo shell. This parameter is called the projection, you can suppress keys as well. But in this instance it is saying, only return to me the 'parent_id' key from the document that is found. However, if you do not suppress the _id column explicitly, it WILL be returned.</p> <p>The second part of your question: This code is assigning the value that is returned from the find query, in this case it will be a document where the _id is equal to the parent_id passed into the function - build_ancestors_full. This document will display the parent key, name key, slut, and ancestors key. parent.pop('parent') will pop a value from the 'parent' key of the parent variable which holds the document I just described. Ancestors is an array, ancestors.append(parent) will append the document I described above to the ancestors array.</p> <p>PHP Equivalent:</p> <pre><code>// get all documents that contain ancestors._id = $bop_id, only return the 'parent_id' field $result = $db-&gt;categories-&gt;find(array('ancestors._id' =&gt; $bop_id), array('parent_id' =&gt; 1)); foreach ($result as $document) { build_ancestors_full($document['_id'], $document['parent_id']); } </code></pre> <p><strong>From your first question - I agree that the use of parent_id is a typo.</strong></p> <p>Part 2 PHP:</p> <pre><code>function build_ancestors_full($id, $parent_id) { $ancestors = array(); while ($parent_id != null) { $parent = $db-&gt;categories-&gt;find_one( array('_id' =&gt; parent_id), array('parent' =&gt; 1, 'name' =&gt; 1, 'slug' =&gt; 1, 'ancestors' =&gt; 1)); $parent_id = $parent['parent']; unset($parent['parent']); // array push $ancestors[] = $parent; } $result = $db-&gt;categories-&gt;update( array('_id' =&gt; _id), array('$set' =&gt; array('ancestors' =&gt; $ancestors ) )); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload