content_image/800768.jpg
<img src='content_image/800768.jpg'>

• You can query to retrieve the whole tree, sorting by the field path : 

db.categories.find().sort( { path: 1 } ) 

• You can use regular expressions on the path field to find the descendants of Programming : 

db.categories.find( { path: /,Programming,/ } ) 

• You can also retrieve the descendants of Books where the Books is also at the topmost level of the hierarchy: 

db.categories.find( { path: /^,Books,/ } ) 

• To create an index on the field path use the following invocation: 

db.categories.createIndex( { path: 1 } ) 

This index may improve performance depending on the query: 

– For queries from the root Books sub-tree (e.g. http://docs.mongodb.org/manual/^,Books,/ or http://docs.mongodb.org/manual/^,Books,Programming,/ ), an index on the path field improves the query performance significantly. 

– For queries of sub-trees where the path from the root is not provided in the query (e.g. http://docs.mongodb.org/manual/,Databases,/ ), or similar queries of sub-trees, where the node might be in the middle of the indexed string, the query must inspect the entire index. 

For these queries an index may provide some performance improvement if the index is significantly smaller than the entire collection. 

