I'm a PHP developer trying to tackle http://docs.mongodb.org/ecosystem/use-cases/category-hierarchy/, but I don't know much about Python.
My first question:
for cat in db.categories.find(
{'ancestors._id': bop_id},
{'parent_id': 1}):
build_ancestors_full(cat['_id'], cat['parent_id'])
Where does 'parent_id' come from? Isn't it suppose to be just 'parent'?
My second question:
def build_ancestors_full(_id, parent_id):
ancestors = []
while parent_id is not None:
parent = db.categories.find_one(
{'_id': parent_id},
{'parent': 1, 'name': 1, 'slug': 1, 'ancestors':1})
parent_id = parent.pop('parent')
ancestors.append(parent)
db.categories.update(
{'_id': _id},
{'$set': { 'ancestors': ancestors } })
I would appreciate a psuedo explanation (or PHP equivalent) of this helper function, mainly the following lines:
parent_id = parent.pop('parent')
ancestors.append(parent)
Thank you!
UPDATE & Answer:
Two errors in the example codes:
The first is 'parent_id' => should be 'parent'
The second is
{'parent': 1, 'name': 1, 'slug': 1, 'ancestors':1})
=> ancestors field should be _id