back
141 comments
This article keeps coming up every once in a while and reminds me of all those “Why JS sucks”, “Never use PHP”, “Java is enterprise only”, “Ruby only works on hobby projects” etc...

But then in real life people built great software with all the above, so I’ll just say a great classic: pick something you know, use it well, build something good, end of story!

No tool will fix wrong assumptions or bad design, we can dive into philosophy here but I’m more of a practical person so... :)

This advice is fine for programming languages, but not for data stores. Using them incorrectly (or in MongoDB's case and sometimes MySQL's, correctly) could lead to things like data loss or crashed servers. Simply building your product on one of them is not proof they are good enough.
> “Why JS sucks”, “Never use PHP”, “Java is enterprise only”, “Ruby only works on hobby projects”

All of these have at least a little bit of truth to them, and you should know about the downsides of technologies, even if you decide to use them anyways.

I would argue that a lot of weaker elements in the stack (e.g. PHP) work well because of a stronger database.

A good database system does a lot to catch errors (including consistency problems), isolate them, and roll them back. Moreover, it will allow you to move performance problems into the database, where it's likely to be handled more efficiently with less application code (e.g. joining in the database is likely to be more efficient than a naive join algorithm implemented in the application).

Some will argue that these are misfeatures and should be handled in the application. In some cases, that is true; but you are probably going to need some other aspects of the stack to be very robust and performant to get reasonable results.

In other words (please excuse my examples as they are intended for illustration and not flamebait), PHP over Posgres might be fine; Haskell over MongoDB might be fine; but PHP over MongoDB is playing with fire.

I'd still say that, in most cases, the database layer is the first place to start to work toward a robust system. Even a proven-correct Haskell program can fail miserably if there was a minor bug three versions ago that wrote some bogus data that wasn't caught by a good database layer.

The only practical application of MongoDB I can justify to this day, is if you have a form builder, that allows users to build completely custom forms. Forms.io gives you the form schema in such a scenario as JSON out of the box. That gets matched with answers as JSON.

Saving this to MongoDB directly, rather than SQL seems to simplify things.

With anything else, at the end of the day, you are enforcing FK constraints anyway, so might as well use SQL.

I never had issues with MongoDB performance.

One caveat to this is that I am yet to see a project that needs database sharding in real life, and I have worked on projects with millions of entries in a table and hundreds of writes a minute.

It really depends. Things can be built despite of the technology. I saw this especially at an employer who was heavily invested in golang. Countless times I've thought to myself that they wouldn't be having the issues they were having, sunken costs, reinventing the wheel, etc. if they used a proven technology like the JVM instead of drinking the kool aid and using the latest fad of the day. Tons of money was sunken into it, and it was made to work by force, but not everyone would be able to bear that cost, and it still causes massive inefficiencies due to poor tooling.
It would be more useful if you addressed the specific issues brought up in the article, rather than generically tried to dismiss all articles critical of any programming language...
In each of those language examples, there are examples of great software that migrated away from those languages for different reasons.
Your post implies that there is no tool on earth that "sucks" and that it's not the tool, it's the person.

It's impossible for EVERY tool to be good. This isn't reality. There has to be tools that are patently bad to use and people have used these bad tools to build great things. But it doesn't change the fact that a tool can be horrible to use.

I would argue that at the time the article was written, Mongo was definitively a bad tool. Things have changed, but not all things.

>No tool will fix wrong assumptions or bad design, we can dive into philosophy here but I’m more of a practical person so... :)

And no design can fix a bad tool, we can dive into the practicalities here but I'm more of a philosophical person so...:)

Despite having used document oriented databases for many years(largely because they were shoved down my throat and I inherited someone else's architecture), I never really managed to figure out why people find them so compelling. There has been a shift in the last two years and people have started running away from them. Specifically the web-dev crowd adored them and I guess it's easy to fetch a document in the exact structure you need it but sooner or later you inevitably reach the point where you have to analyze data. And here mongo(and all the similar alternatives) become the biggest pain in the a...neck you can think of. Couchbase tried to tackle this issue with n1ql to a certain degree but at large scale it is still not particularly useful. To my mind, having a relational database which has a good architecture can't be matched by any document oriented database. But getting a large system/database right does take more effort. There are numerous ways to make relational databases incredibly scalable but again, it takes a lot more effort.
There was a time where adding a column to a database was a really big deal. You had to get it past the DBA, and there were real resource constraints on the database system. With a document store the schema is entirely in the hands of the developer.

Also JSON became the standard way to ship data around, and RDBMs systems of the time couldn't really handle JSON. So you either write a bunch of code to map complex nested JSON to relational tables, or just dump it into an un-indexible text column.

There was vendor hype, just like there was around Object databases in the pre-internet days.

If you were starting a new project you needed to decide if you were going to use a document store and an RDBMS or just on or the other. If it was just one you would choose a document store if you anticipated you would need to handle a lot of unstructured data.

Today the situation is revered. A document store only does documents well. A good hybrid database like postgres gives you the best of both worlds. Throw in hosted database services and resource constraints are much less of an issue. So people aren't running back to an old school RDBMS. They are moving to a much superior and evolved data store.

> why people find them so compelling

My theory is that it's easy to add a field by adding logic into the app instead of munging tables relationships. Moves the logic to where developers are more comfortable. Scalability/etc is irrelevant for most use cases anyway.

It's amazing for three things: search, logging and draft records.

Search, with mongodb can do $all query, which is hard to replicate at sql level without aggregation. However I'm still waiting for aggregate-level $elemAt.

Logging, you can attach anything to a property, then it'll be queryable.

Draft records, it's easy to just insert and insert the records because it's schema-less. Validate during creation and validate again during publishing or approval. It's queryable and you can use a generic collection for that.

For logging and draft records, sql JSON field may be able to handle them, though I don't know how good it is at querying.

I have found myself enjoying using a document database as the online store, and then using a 'big data solution' (we use Presto) for any analytics queries later.

Traditional migrations for relational databases are really painful. Document databases make this much easier, and if you've faced the operational pain of needing to migrate a large database (for example, it's so easy to accidentally lock an entire table in Postgres), you might be pretty compelled.

(That said, I think the pendulum is swinging back away from document databases. So you're in luck ;))

I have found 2 use cases, one of which I've never actually seen in the wild.

The most common use case is, "I need to store data where the schema is unknown or can change without notice, and have my shit not break." This is what we used Mongo for.

The other use case I could see (and this is pretty much only with Dynamo) is, "I want to build an application that's cross-region native. Most of my data is relatively static, so I accept eventual consistency on changes. I will have a separate data store for transactional data and data that cannot be eventually consistent." I want to build this project, but it will never happen because it's too easy to RDBMS in a single region to start.

> Despite having used document oriented databases for many years(largely because they were shoved down my throat and I inherited someone else's architecture), I never really managed to figure out why people find them so compelling.

Well, filesystems are pretty good. It's the only document store I use (and mostly enjoy).

But then you look at the trade-off with some think like just Maildir, and you really start to wonder if this schemaless document store thing is so great?

I suppose the real shame is that proper object dbs like zodb or gemstone gets much less attention - they to have big trade-offs - but I feel they at least give back in terms of consistency and simplicity.

> I never really managed to figure out why people find them so compelling.

This might sound jaded but my feeling is that a lot of developers just looked at JSON objects that they were already working with and thought to themselves "actually, it would be cool to just store this directly".

Which, in itself, isn't a bad idea but writing a completely new solution from scratch to a problem that's been solved for decades seems a bit like hubris.

AFAIK many relational databases support JSON today, so I'm not sure what the argument would be to choose something like MongoDB today from scratch if you had the choice of anything.

We are using mongo specifically because it makes it easy to do analytics on large datasets quickly.
> On my laptop, PostgreSQL takes about a minute to get denormalized data for 12,000 episodes, while retrieval of the equivalent document by ID in MongoDB takes a fraction of a second.

What? Her database can't possibly be indexed properly.

Yeah, this sounds like a design defect. But since the author doesn't really describe what they did, it is hard to really figure it out. I'm guessing this is some sort of query with a self-join going on, where the mongo request is a basic fetch by id.
I use MongoDB in my application. The approach I took is to store data in flat documents (relational style) and only de-normalize when necessary for performance. The relational model was invented for a reason -- it is flexible and it is easy to update data in one place and so on. The downside of relational is that joins will kill you when you have very large tables. To avoid joins, I use lookups when possible, and de-normalize only to the extend needed. I get the best of both worlds.
I first read this article while working on a project where the company had basically written a RDBMS using MongoDB. It was so many different kinds of bad I lost count.
This article from 8 years ago highlights how far MongoDB has come: transactions, left outer joins ($lookup), etc.
This article appears here pretty frequently: https://hn.algolia.com/?dateRange=all&page=0&prefix=false&qu...
As a sysadmin that often gets the privilege of pretending to design the smallest of complex systems, one of my regular components has been CouchDB because of its built in http api.

I've never been anti-Mongo, but this one little piece has made CouchDB an affordable choice for people like me who are not equipped to otherwise defend the choice.

Is there a missing piece that could deal Mongo back in the next time I try to convince someone that there's as straight a path to the sysadmin solutions I generally compose?

We've used MongoDB at our SaaS and have grown to well over $1m ARR and never had an issue.

Maybe if you're trying to build a massive ($B) company, starting with PostgreSQL makes more sense for you. For everything else, MongoDB works just fine.

Just use the technologies that you know and can move fastest with. Startups rarely succeed/fail because of which technologies you choose to use.

Mongo and other nosql databases are still the absolute fastest way to get started particularly you don't know what your data is eventually going to look like.
I'll never ever use MongoDB again, because every single time I've ended up running a cluster it has always been the most troubled part of my stack. I've been burned way too many times to ever considering touching that stove again.
I think there are two separate aspects that get conflated into one.

1) Document database - rather than a strict rigid schema, you can store nested json documents in tables/collections. Or the idea of soft schema where the whole database doesn't need to be blocked for a schema change and you have some leeway in integrity.

2) Relational database - Ability to make complex sql queries that join data from multiple tables.

Mongodb has some support for joining but it doesn't have a sql variant. If your data is mostly key:val store then it's great. You can shard it, and have replicas. It's easy to make a fast reliable backend with mongodb. Many popular sites run on mongodb backend.

However with new json types in MySQL and Postgres, it too has support for inserting documents and querying subkeys. It can be sharded and replicated (albeit with a bit more configuration).

Couchbase which is like mongo (in its document store capabilities) N1QL which offers agility of SQL and flexibility of JSON.

So like any tool, it has it's tradeoffs.

Then again kudus to the author for evoking our reptillian brains: "Never use MongoDB" incites emotions and gets you on top of HN. If it was called "When to use MongoDB", it wouldn't get the same reaction.

> On a social network, however, nothing is that self-contained. Any time you see something that looks like a name or a picture, you expect to be able to click on it and go see that user, their profile, and their posts. A TV show application doesn’t work that way. If you’re on season 1 episode 1 of Babylon 5, you don’t expect to be able to click through to season 1 episode 1 of General Hospital.

That is exactly what I'd expect, and that is how small websites like IMDB work. I am on the page for a General Hospital episode, and via the actors in the episode or whatever other part I can click through to Babylon 5, or the other way around, or anywhere else.

> But there are actually very few concepts in the world that are naturally modeled as normalized tables. We use that structure because it’s efficient, because it avoids duplication, and because when it does get slow, we know how to fix it.

Urmmm...How?

MongoDB + Ruby sparks joy for me. It's come a long way since 2013 and latest features like transactions (though nowhere near SQL level of robustness) are enough for my use cases. To each his or her own.
In the early tens we ran an engineering project to improve critical sections in applications using transactional memory. We had a PhD level intern applying our techniques to various open source projects. One target was MongoDB. After a few days of investigation of the Mongo source code, he had to give up because he couldn't even find the critical sections in the source. They had locking, but it was extremely convoluted.

So yes I would agree with that. Never use MongoDB.

Jepsen review 2020:

https://jepsen.io/analyses/mongodb-4.2.6

... not good.

I'm not a fan of MongoDb (although I have used it for many years) but it has its place in the market and there is need for it. "Never use MongoDb" is a clickbate title. I really wish I could down votes this post but I can't. I can only upvote unfortunately.
Ok, so besides the technical faults of Mongo within the context of its category, what is the ideal use-case of a document oriented store?

If you use metadata documents to model your relations you might get away with the most dangerous foot guns, but then why not jump straight into graph databases?

I'm by no means a fan of Mongo, but the product has improved quite a lot over the years.

Mongo now supports multi-document (and multi-node) transactions, joins, and has a decent storage engine.

So you might even have a chance of keeping your data actually consistent.

Unless you want to.
SQL is a solution to a problem in the vein of prematurely optimizing for many use cases where mongodb is called for.
For the record we use mdb in production, and it's been fine.
TLDR never use software architects who don't realize how MongoDB is a really bad fit for their needs. Mistakes are possible, but not thinking about requirements is negligence.
plot twist .. use MongoDB api /drivers on Document Layer on FoundationDB ;-)