Database Reference
In-Depth Information
conn = pymongo . MongoClient ( read_preference = pymongo . SECONDARY_PREFERRED )
If you wish to restrict reads to only occur on the secondary, you can use SECONDARY instead:
conn = pymongo . MongoClient ( read_preference = pymongo . SECONDARY )
You can also specify read_preference for specific queries, as shown here:
results = db . product . find ( ... , read_preference = pymongo . SECONDARY_PREFERRED )
Or:
results = db . product . find ( ... , read_preference = pymongo . SECONDARY )
Category Hierarchy
One of the issues faced by product catalog maintainers is the classification of products.
Products are typically classified hierarchically to allow for convenient catalog browsing and
product planning. One question that arises is just what to do when that categorization changes.
This use case addresses the construction and maintenance of a hierarchical classification sys-
tem in MongoDB.
Solution Overview
To model a product category hierarchy, our solution here keeps each category in its own doc-
ument with a list of the ancestor categories for that particular subcategory. To anchor our ex-
amples, we use music genres as the categorization scheme we'll examine.
Since the hierarchical categorization of products changes relatively infrequently, we're more
concerned here with query performance and update consistency than update performance.
Schema Design
Our schema design will focus on the hierarchy in Figure 5-1 . When designing a hierarchical
schema, one approach would be to simply store a parent_id in each document:
Search WWH ::




Custom Search