Database Reference
In-Depth Information
category = db . categories . find (
{ 'slug' : slug },
{ '_id' : 0 , 'name' : 1 , 'ancestors.slug' : 1 , 'ancestors.name' : 1 } )
In order to make this query fast, we just need an index on the slug field:
>>>
>>> db . categories . ensure_index ( 'slug' , unique = True )
Add a category to the hierarchy
Supposewewantedtomodifythehierarchybyaddinganewcategory,asshownin Figure 5-2 .
This insert operation would be trivial if we had used our simple schema that only stored the
parent ID:
doc = dict(name='Swing', slug='swing', parent=ragtime_id)
Figure 5-2. Adding Swing to the hierarchy
Since we are keeping information on all the ancestors, however, we need to actually calculate
this array and store it after performing the insert. For this, we'll define the following
build_ancestors helper function:
def
def build_ancestors ( _id , parent_id ):
parent = db . categories . find_one (
Search WWH ::




Custom Search