MongoDB五種樹形結構表示法

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 } )

特徵:3d

  1. 快速獲取父節點:code

    db.categories.findOne( { _id: "MongoDB" } ).parentblog

  2. 方便建立父節點索引排序

    db.categories.ensureIndex( { parent: 1 } )索引

  3. 經過查詢父節點獲取兒子節點隊列

    db.categories.find( { parent: "Databases" } )im

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" ] } )

特徵:sort

  1. 快速獲取兒子節點db

    db.categories.findOne( { _id: "Databases" } ).children

  2. 方便建立子節點索引

    db.categories.ensureIndex( { children: 1 } )

  3. 經過查詢兒子節點獲取父節點

    db.categories.find( { children: "MongoDB" } )

  4. 適合存儲存儲圖,一個節點可能有多個父母。

第三種:祖先隊列結構

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 } )

特徵:

  1. 快速獲取祖先

    db.categories.findOne( { _id: "MongoDB" } ).ancestors

  2. 方便建立祖先節點索引

    db.categories.ensureIndex( { ancestors: 1 } )

  3. 經過查詢祖先來獲取後代

    db.categories.find( { ancestors: "Programming" } )

  4. 祖先模式略慢於物化路徑模式

第四種:物化路徑結構

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," } )

特徵:

  1. 經過查詢檢索排序

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

  2. 快速查找子節點

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

  3. 爲路徑建立索引

    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 } )

特徵:

  1. 快速獲取子節點

    var databaseCategory = db.categories.findOne( { _id: "Databases" } ); db.categories.find( { left: { $gt: databaseCategory.left }, right: { $lt: databaseCategory.right } } );

  2. 內容修改很低效適合靜態樹。

相關文章
相關標籤/搜索