Stop Claiming that youâre Using a Schemaless Database
One of MongoDBâs arguments when evangelising MongoDB is the fact that MongoDB is a âschemalessâ database:
Why Schemaless?
MongoDB is a JSON-style data store. The documents stored in the database can have varying sets of fields, with different types for each field.
And thatâs true. But it doesnât mean that there is no schema. There are in fact various schemas:
- The one in your head when you designed the data structures
- The one that your database really implemented to store your data structures
- The one you should have implemented to fulfill your requirements
Every time you realise that you made a mistake (see point three above), or when your requirements change, you will need to migrate your data. Letâs review again MongoDBâs point of view here:
With a schemaless database, 90% of the time adjustments to the database become transparent and automatic. For example, if we wish to add GPA to the student objects, we add the attribute, resave, and all is well â if we look up an existing student and reference GPA, we just get back null. Further, if we roll back our code, the new GPA fields in the existing objects are unlikely to cause problems if our code was well written.
Everything above is true as well.
âSchema-lessâ vs. âSchema-fulâ
But letâs translate this to SQL (or use any other âschema-fulâ database instead):
ALTER TABLE student ADD gpa VARCHAR(10);
And weâre done! Gee, weâve added a column, and weâve added it to ALL rows. It was transparent. It was automatic. We âjust get back nullâ on existing students. And we can even âroll back our codeâ:
ALTER TABLE student DROP gpa;
Not only are the existing objects unlikely to cause problems, we have actually rolled back our code AND database.
Letâs summarise:
- We can do exactly the same in âschema-lessâ databases as we can in âschema-fulâ ones
- We guarantee that a migration takes place (and itâs instant, too)
- We guarantee data integrity when we roll back the change
What about more real-world DDL?
Of course, at the beginning of projects, when they still resemble the typical cat/dog/pet-shop, book/author/library sample application, weâll just be adding columns. But what happens if we need to change the student-teacher 1:N relationship into a student-teacher M:N relationship? Suddenly, everything changes, and not only will the relational data model prove far superior to a hierarchical one that just yields tons of data duplication, itâll also be moderately easy to migrate, and the outcome is guaranteed to be correct and tidy!
CREATE TABLE student_to_teacher AS SELECT id AS student_id, teacher_id FROM student; ALTER TABLE student DROP teacher_id;
⊠and weâre done! (of course, weâd be adding constraints and indexes)
Think about the tedious task that youâll have transforming your JSON to the new JSON. You donât even have XSLT or XQuery for the task, only JavaScript!
Letâs face the truth
Schemalessness is about a misleading term as much as NoSQL is:
https://twitter.com/edd/status/400190499585544192
And again, MongoDBâs blog post is telling the truth (and an interesting one, too):
Generally, there is a direct analogy between this âschemalessâ style and dynamically typed languages. Constructs such as those above are easy to represent in PHP, Python and Ruby. What we are trying to do here is make this mapping to the database natural.
When you say âschemalessâ, you actually say âdynamically typed schemaâ â as opposed to statically typed schemas as they are available from SQL databases. JSON is still a completely schema free data structure standard, as opposed to XML which allows you to specify XSD if you need, or operate on document-oriented, âschema-lessâ (i.e. dynamically typed) schemas.
(And donât say thereâs json-schema. Thatâs a ridiculous attempt to mimick XSD)
This is important to understand! You always have a schema, even if you donât statically type it. If youâre writing JavaScript, you still have types, which you have to be fully aware of in your mental model of the code. Except that thereâs no compiler (or IDE) that can help you infer the types with 100% certainty.
An example:
https://twitter.com/lukaseder/status/522311918384857088
⊠and more:
So, thereâs absolutely nothing that is really easier with âschemalessâ databases than with âschemafulâ ones. You just defer the inevitable work of sanitising your schema to some other later time, a time when you might care more than today, or a time when youâre lucky enough to have a new job and someone else does the work for you. You might have believed MongoDB, when they said that âobjects are unlikely to cause problemsâ.
But let me tell you the ugly truth:
Anything that can possibly go wrong, does
– Murphy
We wish you good luck with your dynamically typed languages and your dynamically typed database schemas â while weâll stick with type safe SQL.
| Reference: | Stop Claiming that youâre Using a Schemaless Database from our JCG partner Lukas Eder at the JAVA, SQL, AND JOOQ blog. |



Not sure what size db you have but alter table in mysql is far from instant and copies the whole table. Whilst I get the point you are trying to make try to be accurate.
I also enjoy the simplified no downtime deployment you get with mongo for example. Schema migrations in mysql greatly complicate continuous delivery and can make downtime difficult to avoid.
no one cares :)