Database Reference
In-Depth Information
{ '_id' : parent_id },
{ 'name' : 1 , 'slug' : 1 , 'ancestors' : 1 })
parent_ancestors = parent . pop ( 'ancestors' )
ancestors = [ parent ] + parent_ancestors
db . categories . update (
{ '_id' : _id },
{ '$set' : { 'ancestors' : ancestors } })
Note that you only need to travel up one level in the hierarchy to get the ancestor list for
“Ragtime” that you can use to build the ancestor list for “Swing.” Once you have the parent's
ancestors, you can build the full ancestor list trivially. Putting it all together then, let's insert a
new category:
doc = dict ( name = 'Swing' , slug = 'swing' , parent = ragtime_id )
swing_id = db . categories . insert ( doc )
build_ancestors ( swing_id , ragtime_id )
Change the ancestry of a category
This section addresses the process for reorganizing the hierarchy by moving “Bop” under
“Swing,” as shown in Figure 5-3 . First, we'll update the bop document to reflect the change
in its ancestry:
db . categories . update (
{ '_id' : bop_id }, { '$set' : { 'parent' : swing_id } } )
Search WWH ::




Custom Search