Five tree-structured representations of MongoDB
First type: Parent link structure
Db. categories. Insert ({_ID: "MongoDB", Parent:"Databases"})Db. categories. Insert ({_ID: "DBM", Parent:"Databases"})Db. categories. Insert ({_ID: "Databases", Parent:"Programming"})Db.categories.insert ({ _id: Languages ", Parent: " Programming "}) db.insert ({_id: " Programming ", Parent: "Books"}) db.categories.insert ( {_id: Span class= "hljs-string" > "Books", Parent:null})
Characteristics:
Quick Get parent node:
Db.categories.findOne ({_id: "MongoDB"}). Parent
Easy to create parent node index
Db.categories.ensureIndex ({parent:1})
Get son node by querying parent node
Db.categories.find ({parent: "Databases"})
4. Multiple queries are required to retrieve subtrees.
Second Type: sub-link structure
Db. categories. Insert ({_ID: "MongoDB", Children: []})Db. categories. Insert ({_ID: "DBM", Children: []})Db. categories. Insert ({_ID: "Databases", Children: ["MongoDB","dbm"]})Db. categories. Insert ({ _id: "Languages", Children: []})db. Categories. Insert ( { _id: " Programming ", Children: [ " Databases ", " Languages "]})db. Categories. Insert ( { _id: "Books", Children: [ "Programming"]})
Characteristics:
Quick Get son node
Db.categories.findOne ({_id: "Databases"}). Children
Easy to create child node index
Db.categories.ensureIndex ({children:1})
Get parent node by querying son node
Db.categories.find ({children: "MongoDB"})
Suitable for storing storage graphs, one node may have multiple parents.
The third type: ancestor queue structure
Db. categories. Insert ({_ID: "MongoDB", Ancestors: ["Books","Programming","Databases"], Parent:"Databases"})Db. categories. Insert ({_ID: "dbm", Ancestors: ["Books","Programming","Databases"], Parent:"Databases"})Db. categories. Insert ({_ID: "Databases", Ancestors: ["Books","Programming"], Parent:"Programming"})Db. categories. Insert ({_ID: "Languages", Ancestors: [ "Books", Span class= "hljs-string" > "programming"], Parent: "Programming"}) Db.categories.insert ({_id: " Programming ", Ancestors: [ "Books"], Parent: "Books"}) db.categories.insert ( {_id: Span class= "hljs-string" > "Books", Ancestors: [], parent:null}) / span>
Characteristics:
Get Ancestors Fast
Db.categories.findOne ({_id: "MongoDB"}). Ancestors
Easy to create ancestor node index
Db.categories.ensureIndex ({ancestors:1})
retrieving descendants by querying ancestors
Db.categories.find ({ancestors: "Programming"})
Ancestor mode is slightly slower than materialized path mode
Fourth type: materialized path structure
Db. categories. Insert ({_ID: "Books", Path:null})Db. categories. Insert ({_ID: "Programming", Path:", Books,"})Db. categories. Insert ({_ID: "Databases", Path:", Books,programming,"})Db. categories. Insert ({_id: Span class= "hljs-string" > "Languages", Path: db.categories.insert ( {_id: Span class= "hljs-string" > "MongoDB", Path: db.categories.insert ({ Span class= "Hljs-rule" >_id: dbm ", Path: ", Books,programming,databases, "})
Characteristics:
Retrieving sorting by query
Db.categories.find (). Sort ({path:1})
Quick Find child nodes
Db.categories.find ({path:/^,books,/}) Db.categories.find ({path:/^,books,/})
To create an index for a path
Db.categories.ensureIndex ({path:1})
Fifth: Set model
Db. categories. Insert ({_ID: "Books", Parent:0, Left:1, right:12})Db. categories. Insert ({_ID: "Programming", Parent:"Books", left:2, right:11})Db. categories. Insert ({_ID: "Languages", Parent:"Programming", left:3, right:4})Db. categories. Insert ({_ID: "Databases", Parent:"Programming", left:5, right: 10}) db. Categories. Insert ( { _id: "MongoDB", Parent: "Databases", left: 6, right: 7})db. Categories. Insert ( { _id: "dbm", Parent: "Databases", left: 8 , right: 9})
Characteristics:
Quickly get child nodes
var databasecategory = Db.categories.findOne ({_id: "Databases"}); Db.categories.find ({left: {$gt: Databasecategory.left}, right: {$lt: Databasecategory.right}});
Content modification is inefficient for static trees.
Five tree-structured representations of MongoDB