I don't have any experience with MongoDB, but the example put me off using it completely. The MySQL query is concise and brief, the MongoDB equivalent is bloated. Only after several seconds I was able to deduce that the MongoDB query probably does something more than MySQL query. Can you please make the examples more comparable? Or did I misunderstand the MongonDB and it actually is so bloated by design? I believe it is not your goal to discourage people from MongoDB , if so, better not do it unintentionally.
>> Or did I misunderstand the MongonDB and it actually is so bloated by design?
Depending on the type of query and the way you designed your database schema MongoDB queries can be either more concise or (in your words) more bloated than SQL. You can't really say either one is more bloated than the other in terms of query syntax.
Personally, I generally prefer MongoDB queries over SQL, because it doesn't have a concept of joins, inner queries, temporary tables, etc, which usually translates to more but simpler queries. If the data model for your applications relies heavily on any of these features, maybe you shouldn't use MongoDB (or any other NoSQL database, for that matter).
The default is a particularly perverse example. Most anything that involves aggregation or on-the-fly computation is better done in MySQL or similar. Typical Mongo usage is predicated on the assumption that you're going to denormalize your data into documents that are trivially queryable. Treating it like a SQL store is a mistake, and this query demonstrates exactly why.
The default query already filled in is translating to the use of a Group function, which is a very bad idea. While not deprecated per se, its use is discouraged.
Group does not function in Sharding mode at all, it also takes a lock on the JavaScript interpreter making it non-parallelizable.
Map/Reduce is somewhat better in that it is shardable, and with V8 likely in the next stable release, will have better parallelization prospects.
(EDIT) To clarify - Aggregation is ideal because its implementation is 100% in C++, meaning there are no JavaScript interpreter locks necessary to run it, so it is parallelizable. Additionally, one of the biggest overhead costs to MapReduce and Group in MongoDB is the translation back and forth between BSON (the native format MongoDB uses for data, or rather the C++ representations thereof) and JavaScript types. Aggregation not utilizing JavaScript eliminates this overhead and manipulates the database' internal types directly.
Thanks, that's extremely helpful. Based on how this is built, it might not actually be that hard to migrate over to the aggregation framework. I'll take a look.
I noted v8 in my comment; it won't be a panacea. It certainly doesn't fix the encoding/decoding overhead of BSON<->JavaScript, but the JIT and multithreading will help in other areas.
Group still will not become sharding-capable with v8, either.
Very nice! I once thought about doing something like this, but I had some real work to do. Thanks for this resource!
This is specially useful because of the verbosity and ugliness of the "JSON" API (much more difficult to get right by hand than SQL) and because I found 0 working GUI tools to work with Mongo in a mac (they all crash at startup or after ~5 seconds of usage in a modern mac).
Awesome! I haven't played around with NoSQL databases and I always wondered how you would query one. Definitely peaked my interest in the whole movement.
+1 for this. Would be interested in seeing how this is done in code to further my understanding of MongoDB. Would be interesting to create a reverse translator as well.