標籤:blog http os ar 2014 div sp log html
MongoDB五種樹形結構標記法
第一種:父連結結構
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.categories.insert( { _id: "Programming", parent: "Books" } )db.categories.insert( { _id: "Books", parent: null } )
特徵:
快速擷取父節點:
db.categories.findOne( { _id: "MongoDB" } ).parent
方便建立父節點索引
db.categories.ensureIndex( { parent: 1 } )
通過查詢父節點擷取兒子節點
db.categories.find( { parent: "Databases" } )
4.需要多個查詢來檢索子樹。
第二種:子連結結構
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" ] } )
特徵:
快速擷取兒子節點
db.categories.findOne( { _id: "Databases" } ).children
方便建立子節點索引
db.categories.ensureIndex( { children: 1 } )
通過查詢兒子節點擷取父節點
db.categories.find( { children: "MongoDB" } )
適合儲存儲存圖,一個節點可能有多個父母。
第三種:祖先隊列結構
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", "Programming" ], parent: "Programming" } )db.categories.insert( { _id: "Programming", ancestors: [ "Books" ], parent: "Books" } )db.categories.insert( { _id: "Books", ancestors: [ ], parent: null } )
特徵:
快速擷取祖先
db.categories.findOne( { _id: "MongoDB" } ).ancestors
方便建立祖先節點索引
db.categories.ensureIndex( { ancestors: 1 } )
通過查詢祖先來擷取後代
db.categories.find( { ancestors: "Programming" } )
祖先模式略慢於物化路徑模式
第四種:物化路徑結構
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: "Languages", path: ",Books,Programming," } )db.categories.insert( { _id: "MongoDB", path: ",Books,Programming,Databases," } )db.categories.insert( { _id: "dbm", path: ",Books,Programming,Databases," } )
特徵:
通過查詢檢索排序
db.categories.find().sort( { path: 1 } )
快速尋找子節點
db.categories.find( { path: /^,Books,/ } ) db.categories.find( { path: /^,Books,/ } )
為路徑建立索引
db.categories.ensureIndex( { path: 1 } )
第五種:集合模型
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 } )
特徵:
快速擷取子節點
var databaseCategory = db.categories.findOne( { _id: "Databases" } ); db.categories.find( { left: { $gt: databaseCategory.left }, right: { $lt: databaseCategory.right } } );
內容修改很低效適合靜態樹。
MongoDB五種樹形結構標記法