首頁 資料庫 mysql教程 Trees in MongoDB

Trees in MongoDB

Jun 07, 2016 pm 05:56 PM
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实例:

嵌套数据集:

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

navicat過期怎麼辦 navicat過期怎麼辦 Apr 23, 2024 pm 12:12 PM

解決 Navicat 過期問題的方法包括:續約授權;卸載並重新安裝;停用自動更新;使用 Navicat Premium Essentials 免費版;聯絡 Navicat 客戶支援。

前端學nodejs很難嗎 前端學nodejs很難嗎 Apr 21, 2024 am 04:57 AM

對於前端開發人員而言,學習 Node.js 的難度取決於其 JavaScript 基礎、伺服器端程式設計經驗、命令列熟悉度和學習風格。學習過程包括入門級和進階級的模組,重點是基礎概念、伺服器端架構、資料庫整合和非同步程式設計。整體而言,對於具備紮實 JavaScript 基礎並願意投入時間和精力的開發人員,學習 Node.js 並不困難,但對於缺乏相關經驗的人來說,可能需要克服一定的挑戰。

navicat怎麼連mongodb navicat怎麼連mongodb Apr 24, 2024 am 11:27 AM

要使用 Navicat 連接 MongoDB,您需要:安裝 Navicat建立 MongoDB 連接:a. 輸入連接名稱、主機位址和連接埠b. 輸入認證資訊(如果需要)新增 SSL 憑證(如果需要)驗證連線儲存連接

nodejs常用模組有哪些 nodejs常用模組有哪些 Apr 21, 2024 am 04:34 AM

Node.js 中最常用的模組包括:用於檔案操作的檔案系統模組用於網路通訊的網路模組用於處理資料流的流模組用於與資料庫互動的資料庫模組其他實用模組,如加密、查詢字符字串解析和HTTP 框架

net4.0有什麼用 net4.0有什麼用 May 10, 2024 am 01:09 AM

.NET 4.0 用於創建各種應用程序,它為應用程式開發人員提供了豐富的功能,包括:物件導向程式設計、靈活性、強大的架構、雲端運算整合、效能最佳化、廣泛的程式庫、安全性、可擴展性、資料存取和行動開發支援。

nodejs用什麼資料庫好 nodejs用什麼資料庫好 Apr 21, 2024 am 05:06 AM

對於 Node.js 應用,選擇資料庫取決於應用程式要求。 NoSQL 資料庫 MongoDB 提供彈性,Redis 提供高並發性,Cassandra 處理時間序列數據,Elasticsearch 專用於搜尋。 SQL 資料庫 MySQL 效能出色,PostgreSQL 功能豐富,SQLite 輕量級,Oracle Database 全面。選擇時,需考慮資料類型、查詢、效能、事務性、可用性、許可和成本。

nodejs怎麼連接資料庫 nodejs怎麼連接資料庫 Apr 21, 2024 am 05:07 AM

在 Node.js 中連接資料庫的步驟:安裝 MySQL、MongoDB 或 PostgreSQL 套件。建立資料庫連接物件。打開資料庫連接,並處理連接錯誤。

nodejs如何實作資料庫 nodejs如何實作資料庫 Apr 21, 2024 am 05:42 AM

在 Node.js 中連接資料庫需要選擇一個資料庫系統(關係型或非關係型),然後使用特定於該類型的模組建立連接。常見模組包括 mysql(MySQL)、pg(PostgreSQL)、mongodb(MongoDB)和 redis(Redis)。建立連線後,可以使用查詢語句檢索資料並使用更新語句修改資料。最後,完成所有操作後必須關閉連線以釋放資源。遵循這些最佳實務可提高效能和安全性,例如使用連線池、參數化查詢和妥善處理錯誤。

See all articles