Database Reference
In-Depth Information
},
}
Operations
For most deployments, the primary use of the product catalog is to perform search operations.
This section provides an overview of various types of queries that may be useful for support-
ing an ecommerce site. Our examples use the Python programming language, but of course
you can implement this system using any language you choose.
Find products sorted by percentage discount descending
Most searches will be for a particular type of product (album, movie, etc.), but in some situ-
ations you may want to return all products in a certain price range or discount percentage.
For example, the following query returns all products with a discount greater than 25%, sorted
by descending percentage discount:
query = db . products . find ( { 'pricing.pct_savings' : { '$gt' : 25 })
query = query . sort ([( 'pricing.pct_savings' , - 1 )])
To support this type of query, we'll create an index on the pricing.pct_savings field:
db . products . ensure_index ( 'pricing.pct_savings' )
NOTE
Since MongoDB can read indexes in ascending or descending order, the order of the index
does not matter when creating single-element indexes.
Find albums by genre and sort by year produced
Thefollowingreturnsthedocuments forthealbumsofaspecific genre,sortedinreversechro-
nological order:
query = db . products . find ({
'type' : 'Audio Album' ,
'details.genre' : 'jazz' })
Search WWH ::




Custom Search