Should MongoDB Use SQL as a Query Language?

Mar 14 • Posted 4 years ago

MongoDB does not use SQL as a query language.  Why not?  This is a very good question and we have discussed it on the project for a long time.  There are a few reasons for this.

Given the document-oriented nature of the storage, if we were to do SQL, it really world be a variant, not true SQL.  There would be no joins, and we would need extensions to handle the nested constructs involved in JSON storage elegantly.  The extensions wouldn’t be that much but we would need something like the current MongoDB dot notation to reach into objects — something like this perhaps:

SELECT * FROM users WHERE addr.state = ‘NY’

Reaching into arrays would need something too:

SELECT * FROM posts WHERE comments[].author = ‘fred’

In Mongo’s JSON query syntax the above would be:

{ “comments.author” : “fred” }

The term NoSQL is a bit inaccurate - we are really talking about horizontally scalable postrelational stores, not about the query language.  I would consider the Google App Engine Data Store NoSQL, and it uses a SQL-like query language GQL.

The main reason we went the way we did with the query language - representing queries as JSON - was to normalize the data we are storing with the query mechanism.  If we are storing JSON in the database, can we not represent the queries that way too?  We thought that made sense.

I’ve never been a fan of embedding one programming language in another.  Building up strings of SQL dynamically has always seemed a bit strange to me.  I much prefer representing the queries in a data-driven way instead.

blog comments powered by Disqus
blog comments powered by Disqus