37

I am currently writing a thesis and need to display the schema of my MongoDB in a diagram. I have found no resources about diagrams for document-based databases.

There are Entity Relationship Diagrams (ERD) for relational databases. What options do I have for MongoDB? I've noticed that a lot of blogs just display the raw JSON as their "diagram" but this isn't feasible in my thesis.

Here is a sample of one of my JSON structures:

//MultiChoiceQuestion
{
    "title": "How are you?",
    "valid_answers" : [
        {
            "_id" : ObjectID(xxxx),
            "title": "Great",
            "isCorrect": true,
        },
        {
            "_id" : ObjectID(yyyy),
            "title": "OK",
            "isCorrect": false,
        },
        {
            "_id" : ObjectID(zzzz),
            "title": "Bad",
            "isCorrect": false,
        }
    ],
    "user_responses" : [
        {
            "user": ObjectID(aaaa),
            "answer": ObjectID(xxxx)
        },
        {
            "user": ObjectID(bbbb),
            "answer": ObjectID(xxxx)
        },
        {
            "user": ObjectID(cccc),
            "answer": ObjectID(yyyy)
        }
    ]
}

//User
{
    "_id": ObjectID(aaaa),
    "name": "Person A"
}
//User
{
    "_id": ObjectID(bbbb),
    "name": "Person B"
}
//User
{
    "_id": ObjectID(cccc),
    "name": "Person C"
}

Could this be a possible diagram: Possible Diagram?

Community
  • 1
  • 1
Andrew
  • 896
  • 2
  • 12
  • 31
  • since schema only applies to a single document, it might be why everyone just displays the document itself. no other document in the collection has to have the same schema. – Asya Kamsky Jul 04 '12 at 07:13
  • Yes, but in my case every "MultiChoiceDocument" is exactly the same so I would like to show that. – Andrew Jul 04 '12 at 07:16
  • 1
    Even though there is no explicit schema in a MongoDB datastore, there's nearly always still an implicit schema that needs to be represented. http://martinfowler.com/articles/schemaless/ – AndrewW Jun 16 '13 at 08:02

2 Answers2

36

We found class diagrams to actually be one of the best ways to represent a mongo schema design.

It can capture most of the items that a document will have such as arrays, embedded objects and even references.

General guidelines we use to relate onto concepts to uml

Embed = Composition aggregation

Reference = Association class

If you're unfamiliar with the uml terminology then this is a decent intro.

UML intro from IBM site

Prasith Govin
  • 1,267
  • 12
  • 8
  • 1
    Better reference might be https://www.ripublication.com/ijaer17/ijaerv12n5_12.pdf. I found that MongoDB data model is fitted to OOP design rather than ER design. Embedded collection can be modeled as an inner class. Different patterns of documents in a same collection can be designed as different inheriting classes. – Tae-Sung Shin Feb 15 '20 at 07:55
  • Sorry why reference is equal to association class? – Luk Aron Dec 27 '20 at 12:24
4

There is a tool doing diagrams for MongoDb, is called DbSchema. It discovers the schema by scanning data from db. I would also suggest trying two features from them :

  • virtual relations which allow exploring data from different collections in the same time. A kind of JOIN between different collections.
  • HTML documentation, we use it in presentations as well - the comments are in mouse-over ( diarams are saved as vector images ).
DbSchema
  • 413
  • 5
  • 16
  • I highly recommend DbSchema as well. It automatically generates complex and beautiful documentation just by scanning the collections. – Ernani Feb 22 '22 at 13:57