Trees in MongoDB
Trees in MongoDBPosted on 引用地址:%20in%20MongoDB.html 树结构存储最好的方式常常依赖于要执行的操作;下面讨论一下不同的存储方案。在实践中,许多开发人员找到了一些使用起来很方便的模式:单文档存储整根树(Full Tree in single Document),父连接(P
Trees in MongoDB Posted on
引用地址:%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: []}
]}
]
}
优点:
缺点:
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实例:
嵌套数据集:

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

解决 Navicat 过期问题的方法包括:续订许可证;卸载并重新安装;禁用自动更新;使用 Navicat Premium Essentials 免费版;联系 Navicat 客户支持。

要使用 Navicat 连接 MongoDB,您需要:安装 Navicat创建 MongoDB 连接:a. 输入连接名称、主机地址和端口b. 输入认证信息(如果需要)添加 SSL 证书(如果需要)验证连接保存连接

.NET 4.0 用于创建各种应用程序,它为应用程序开发人员提供了丰富的功能,包括:面向对象编程、灵活性、强大的架构、云计算集成、性能优化、广泛的库、安全性、可扩展性、数据访问和移动开发支持。

在无服务器架构中,Java函数可以与数据库集成,以访问和操作数据库中的数据。关键步骤包括:创建Java函数、配置环境变量、部署函数和测试函数。通过遵循这些步骤,开发人员可以构建复杂的应用程序,无缝访问存储在数据库中的数据。

本文介绍如何在Debian系统上配置MongoDB实现自动扩容,主要步骤包括MongoDB副本集的设置和磁盘空间监控。一、MongoDB安装首先,确保已在Debian系统上安装MongoDB。使用以下命令安装:sudoaptupdatesudoaptinstall-ymongodb-org二、配置MongoDB副本集MongoDB副本集确保高可用性和数据冗余,是实现自动扩容的基础。启动MongoDB服务:sudosystemctlstartmongodsudosys

本文介绍如何在Debian系统上构建高可用性的MongoDB数据库。我们将探讨多种方法,确保数据安全和服务持续运行。关键策略:副本集(ReplicaSet):利用副本集实现数据冗余和自动故障转移。当主节点出现故障时,副本集会自动选举新的主节点,保证服务的持续可用性。数据备份与恢复:定期使用mongodump命令进行数据库备份,并制定有效的恢复策略,以应对数据丢失风险。监控与报警:部署监控工具(如Prometheus、Grafana)实时监控MongoDB的运行状态,并

连接到数据库,Node.js 提供了 MySQL、PostgreSQL、MongoDB 和 Redis 等多种数据库连接器包。连接步骤包括:1. 安装相应的连接器包;2. 创建连接池维护可重用连接;3. 建立与数据库的连接。注意:操作为异步,需处理错误,保证安全性,优化性能。

是的,Navicat 可以连接到 MongoDB 数据库。具体步骤包括:打开 Navicat 并创建新的连接。选择数据库类型为 MongoDB。输入 MongoDB 主机地址、端口和数据库名称。输入 MongoDB 用户名和密码(如果需要)。单击“连接”按钮。
