前端工具:MyCodepen】| 【W3School】| 【Bootstrap】| 【MarkDown】| 【正则表达式】| 【图标下载】| 【在线工具】| 【W3标准及兼容】| 【前端中文文档

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

特征:

  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. 内容修改很低效适合静态树。

posted @ 2014-08-21 17:38  FuGardenia  阅读(6721)  评论(0编辑  收藏  举报

关于我们

喜欢编程。
上大学四年,一直在探索。
最终走上前端工程师的不归路。


我的微博:

@WOOEOOBOO

GitHub:

@FuGardenia

关注我们

微信号:MoveClouds
移动互联网,云前端信息传播自媒体。

Simple is beauty,Less is more.

简而美,少即多。