Search code examples
databaseneo4jnosqldiagramflowchart

What database should I use to store diagrams/charts in


I am building a project for one of my classes and need help choosing the proper database.

So I want to create organizational charts online and save them in some database. I know this is technically a tree structure, so I wondered if using a graph database like neo4j would be better than a document JSON database (like MongoDB).

Ideally, I would need to be able to save the node's metadata (like name, occupation, previous node, node location on the page...) in order to be able to open the file later on while keeping the data the same (like the location of the nodes would be the same on the page).

Lastly, I would also need a database to update the nodes quickly when a change is created.

Any help would be tremendous! I don't have much knowledge of NoSQL and graph databases.


Solution

  • Both MongoDB as well as Neo4j should be fine, however both have a few limitations:

    MongoDB:

    • As MongoDB can only store documents, it can not handle relations at all. If you need those, you woud have to store them in the document directly, or must look them up inside your app (i.e. first load object x, then read property x.createdByUser, then load the user with the identifier stored in x.createdByUser, ...).
    • While storing data in MongoDB should be easier, as you basically just import JSON and get the exact JSON back (note: sorting of properties can be different), MongoDB can not create queries as complex or as fast as you could with Cypher / Neo4j.

    Neo4j:

    • Neo4j does not support arrays[1] or objects as property values in nodes or relations, therefore you can store only scalar data types (number, string, datetimes, bool, ...). This limits your data model and you woud likely have to serialize your data format and split it up into many nodes/relations.
    • Neo4j can not store the value NULL; every property set to NULL gets deleted. Use false or an empty string for alternatives.

    I personally use a polyglot architecture in my code, which means that I use multiple databases in parallel. I use Neo4j for the basic structure and advanced cypher query capability, and MongoDB for additional data (arrays, objects). All elements have UUIDs as identifiers, which are set in all databases. A bit harder to use than a single database, but once your insert/update/delete code works across them, it's super useful to benefit from every database's primary advantage.

    Don't underestimate the query advantage Neo4j/Cypher provides; I would recommend you to experiment with Neo4j a bit, maybe create a manual dataset etc. In the beginning you can also test it online: https://neo4j.com/cloud/platform/aura-graph-database/

    Note 1: As Christophe Willemsen pointed out, Neo4j does support arrays as properties. Although they are not as flexible as generic arrays like in JSON, as Neo4j's implementation requires all properties of the Array to be the same type. Mixed numbers, i.e. integers and floats, will all be converted to floats.