I'm trying to figure out if the use of schemas on non-relational schemaless databases like MongoDB is a recommended practice or a bad practice to be avoided.

I am doing research work and am looking for objective arguments in favor or against this practice.

  • 1
    Can you give some examples of what you are trying to do? – mcknz Nov 16 '19 at 23:01
  • I'm doing a research work (trying to justify something), but i don't find any trustworthy source to cite that says if using schemas in a schemaless non-relational database is a good or a bad practice. – Javier Scars Nov 16 '19 at 23:08
  • I took the liberty to rephrase the question with a more neutral wording, trying to avoid the terms "good" and "bad" which tend to attract subjective arguments. I hope this stays fully in line with your original wording. – Christophe Nov 17 '19 at 1:05

Schemaless is a misleading term:

  • On one side, a SQL database always has a well-defined schema. This schema is fixed. If you want to put more information in a "record", you'd have to alter the database structure to add new columns to a table.

  • On the other side, a document oriented database like MongoDB has no predefined schema: every document inserted into a collection can have a different structure. You can literally save apples and organges together: the DB won't care.

But the truth, is that despite a MongoDB collection could contain anything, in practice there is always some kind of structure in it. Schemaless therefore is rather to be understood as a dynamic or flexible scheme, that does not need the database to be altered to cope with evolutions.

In this context, a schema can be defined for validating documents. This allows to combine the benefits of both worlds:

  • you can define the minimum requirements for a document, for example that a student document must have at least the first and last name since this information is needed by your applications in every display window to designate which student it is processing.
  • you leave all the desired flexibility for the other fields and for evolution of the scheme.

So the use of a schema implies a good practice.

Conversely, not using a schema implies that the documents in a colletion could be complete garbage. In some applications, this could be without consequences. But for general business applications, it sounds more like a bad practice.

Of course, you could reduce the impact of such practice by controlling upfront the structure of the documents you insert. But this would transfer the responsibility to the application and might not prevent that other applications do not comply with the requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.