设为首页 收藏本站
查看: 716|回复: 0

[经验分享] Trees in MongoDB

[复制链接]

尚未签到

发表于 2015-7-7 10:48:28 | 显示全部楼层 |阅读模式
  引用地址:http://api.mongodb.org/wiki/current/Trees%20in%20MongoDB.html
  
  树结构存储最好的方式常常依赖于要执行的操作;下面讨论一下不同的存储方案。在实践中,许多开发人员找到了一些使用起来很方便的模式:“单文档存储整根树(Full Tree in single Document)”,“父连接(Parent Links)”和“祖先数组(Array of Ancestors)”。

1         模式

1.1       单文档存储整根树(Full Tree in Signle Document)
  {
    comments: [
      {by: "mathias", text: "...", replies: []}
      {by: "eliot", text: "...", replies: [
        {by: "mike", text: "...", replies: []}
      ]}
    ]
  }
  优点:


  • 单文档来获取每页
  • 整根树存储在磁盘的同一位置
  • 很容易查看整个树结构
  缺点:


  • 搜索困难
  • 获取局部结果困难
  • 对于大型树结构读取难处理,进一步来说,MongoDB v1.8对于文档大小有限制,16MB(未来版本可能会提高限制)。

1.2       (父连接)Parent Links
  用单个集合来存储所有节点,每个节点包含他父节点的ID,是一种简单的解决方案。这种方法最大的问题是获取完整子树时需要查找多次数据库(或使用db.eval函数)。

> t = db.tree1;

> t.find()
{ "_id" : 1 }
{ "_id" : 2, "parent" : 1 }
{ "_id" : 3, "parent" : 1 }
{ "_id" : 4, "parent" : 2 }
{ "_id" : 5, "parent" : 4 }
{ "_id" : 6, "parent" : 4 }

> // find children of node 4
> t.ensureIndex({parent:1})
> t.find( {parent : 4 } )
{ "_id" : 5, "parent" : 4 }
{ "_id" : 6, "parent" : 4 }
1.3       (子链接)Child Links
  另一种选择是在每个节点文档中存储所有子节点的ID。这个方法是有限制的,如果不操作完整子树是没有问题。他可能也是用于存储一个节点有多个父节点情况的最有效方法。

> t = db.tree2
> t.find()
{ "_id" : 1, "children" : [ 2, 3 ] }
{ "_id" : 2 }
{ "_id" : 3, "children" : [ 4 ] }
{ "_id" : 4 }

> // find immediate children of node 3
> t.findOne({_id:3}).children
[ 4 ]

> // find immediate parent of node 3
> t.ensureIndex({children:1})
> t.find({children:3})
{ "_id" : 1, "children" : [ 2, 3 ] }
1.4       (祖先数组)Array of Ancestors
  在这种方法中将一个节点的所有祖先节点存储到一个数组中。这使得类似于“获取X节点的所有子节点”的操作快且容易。

> t = db.mytree;

> t.find()
{ "_id" : "a" }
{ "_id" : "b", "ancestors" : [ "a" ], "parent" : "a" }
{ "_id" : "c", "ancestors" : [ "a", "b" ], "parent" : "b" }
{ "_id" : "d", "ancestors" : [ "a", "b" ], "parent" : "b" }
{ "_id" : "e", "ancestors" : [ "a" ], "parent" : "a" }
{ "_id" : "f", "ancestors" : [ "a", "e" ], "parent" : "e" }
{ "_id" : "g", "ancestors" : [ "a", "b", "d" ], "parent" : "d" }

> t.ensureIndex( { ancestors : 1 } )

> // find all descendents of b:
> t.find( { ancestors : 'b' })
{ "_id" : "c", "ancestors" : [ "a", "b" ], "parent" : "b" }
{ "_id" : "d", "ancestors" : [ "a", "b" ], "parent" : "b" }
{ "_id" : "g", "ancestors" : [ "a", "b", "d" ], "parent" : "d" }

> // get all ancestors of f:
> anc = db.mytree.findOne({_id:'f'}).ancestors
[ "a", "e" ]
> db.mytree.find( { _id : { $in : anc } } )
{ "_id" : "a" }
{ "_id" : "e", "ancestors" : [ "a" ], "parent" : "a" }
  ensureIndex和MongoDB的multikey特性可以使上面的查询更高效。
  除了祖先数组,我们也存储了节点的直接父节点,使得查找节点的直接父节点更容易。

1.5       物化路径(Materialized Path[Full Path in Each Node))
  物化路径使得对树的特定查询容易。我们在每个节点中存储文档在树中位置的全路径。通常情况下上面提到的“祖先数组”方法都工作很好;当不得不处理字符串建造、正则表达式,字符逃逸,物化路径更容易。(理论上,物化路径将会更快。)
  MongoDB实现物化路径最好的方式是将路径存储成字符串,然后采用正则表达式查询。以“^”开头的正则表达可以被高效执行。把数据看作一个字符串,你需要选择一个分隔符,我们采用“,”。举例:

> t = db.tree
test.tree

> // get entire tree -- we use sort() to make the order nice
> t.find().sort({path:1})
{ "_id" : "a", "path" : "a," }
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }
{ "_id" : "e", "path" : "a,e," }
{ "_id" : "f", "path" : "a,e,f," }
{ "_id" : "g", "path" : "a,b,g," }

> t.ensureIndex( {path:1} )

> // find the node 'b' and all its descendents:
> t.find( { path : /^a,b,/ } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }

> // find the node 'b' and its descendents, where path to 'b' is not already known:
> nodeb = t.findOne( { _id : "b" } )
{ "_id" : "b", "path" : "a,b," }
> t.find( { path : new RegExp("^" + nodeb.path) } )
{ "_id" : "b", "path" : "a,b," }
{ "_id" : "c", "path" : "a,b,c," }
{ "_id" : "d", "path" : "a,b,d," }
{ "_id" : "g", "path" : "a,b,g," }
  Ruby实例:http://github.com/banker/newsmonger/blob/master/app/models/comment.rb
  嵌套数据集:
  http://ar.rubyonrails.org/classes/ActiveRecord/Acts/NestedSet/ClassMethods.html
  这种模式适用于很少需要修改数据集中文档的情况。

2         用例(Use Cases)

2.1       根据局部路径查找节点(Find Nodes by a Partial Path)
  假设我们想要查找树中的给定节点,给出从树的某部分到这个点的路径,然后再到这个点,也许就向下面描述的一样。
  用物化路径方法,我们可以实现上面所提到这种情况。如果“a..b..c..d..e”是到文档的路径,我们想要查找文档用“..b..c..d..”,主要做的事情是使操作快。如果我们是从最上面开始他是很容易的(就像在物化路径部分描述的)。一种方法是使用物化路径加上祖先数据,像这样:
  { path : ",a,b,c,d,e,",  ancestor : ['a','b','c','d','e'] }
  我们可以在祖先上创建multikey索引。然后我们会做一个查找“..b,c,d..”路径上节点的查询,像这样:
  find({ path : /,b,c,d,/, ancestor : 'd',  })
  上面提到祖先的索引会被使用,仅仅“d”以下的文档会被检查。接下来尝试更好的依赖聪明的查询优化。
  find( { path : /,b,c,d,/, ancestor : { $all : ['a','d'] }, ... } )

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-84054-1-1.html 上篇帖子: MongoDB副本集配置系列二:配置MongoDB副本集 下篇帖子: asp.net mvc 4 web api,mongodb,上手,期待更进一步
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表