MongoDB五種樹形結構標記法

來源:互聯網
上載者:User

標籤: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 } )

特徵:

  1. 快速擷取父節點:

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

  2. 方便建立父節點索引

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

  3. 通過查詢父節點擷取兒子節點

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

特徵:

  1. 快速擷取兒子節點

    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. 內容修改很低效適合靜態樹。

MongoDB五種樹形結構標記法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.