Design Pattern for Oriented Acyclic Graphs in MongoDB

Problem

As usual, the problem is displaying an acyclic graph in the database. The choice for the database I had was a relational database such as mysql or mongodb. I chose mongoDb because the DAG in the relational databases is a mess , but if there is a trick that I just could not find, tell me.

The goal is to map DAGs in one or more MongoDB documents. Because we have several children and parents of SubDocuments where there is no possibility. I came across several design patterns, but am not sure who is best to go with.


Tree structure with an array of ancestors

The Ancestors Array offered by mongoDB docs . And this is pretty easy to understand. As far as I understand, my document will look like this:

{ "_id" : "root", "ancestors" : [ null ], "left": 1 } { "_id" : "child1", "ancestors" : [ "root" ], "left": 2 } { "_id" : "child2", "ancestors" : [ "root", "child1" ], "left": 1 } 

This allows me to find all children of this type:

 db.Tree.find({ancestors: 'root'}).sort({left: -1}) 

and to all parents:

 db.Tree.findOne({_id: 'child1'}).ancestors 

DBRefs instead of rows

My second approach would be to replace the string keys with DBRef s. But with the exception of longer database entries, I don't see many advantages over an ancestral array.

String array with children and parents

The final idea is to store not only children for each document, but also parents . This will give me all the opportunities I want. The downside is the massive overhead information that I would create by storing all relationships twice. In the future, I am concerned about the amount of administration. For example. if the document is deleted, I have to check everything else for the link in several fields.


My questions

  • Is MongoDb the right choice for a relational database for this purpose?
  • Are there any flaws / flaws to any of my templates that I missed?
  • Which template would you suggest and why? Perhaps you have experience with one of them?
+7
mongodb nosql database-design
source share
1 answer

Why aren't you using a graph database? Check out ArangoDB, you can use documents like MongoDB, as well as graphics. MongoDB is a great database, but not for storing graphically oriented documents. ArangoDB does.

https://www.arangodb.com/

+1
source share

All Articles