* How does MongoDB support SQL-Like Queries

 

Keywords:MongoDB SQL

It is often seen that someone pursues the solution of MongoDB supporting SQL and gets the following negative answers:

* Go and write the script of MongoDB syntax.

* Mongodb is classified as "NoSql". Some people interpret it as "not only SQL", while others directly understand it as "no SQL"

* Ten years ago, there was a GitHub project Mongo jdbc driver, which has been in the experimental stage. The goal of the project is to translate the basic SQL into MongoDB script syntax, but it didn't work out.

It seems that this is really a very difficult task! Otherwise, the official should have built in SQL support; even if it is not built in, many open source contributors should also have provided available patches from outside.

The data model of relational database is simple, which is a single-layer two-dimensional table with distinct rows and columns; therefore, the calculation language (SQL) describing it is relatively simple; and MongoDB is a multi-layer nested structure, and attribute fields appear arbitrarily. It's not easy to describe which field information of which layer is selected clearly. In addition to further grouping, aggregation, condition and sorting operations, the complexity increases exponentially. This is also the root cause of the difficulty in computing based on MongoDB database for many years.

If we use esProc SPL language that fully supports set operations, it would be easier to deal with this multi-layer nested data structure. Look at the code directly:


A

2

=mongo_shell(A1,"computer.find()").fetch()

3

=A2.new(_id:ID,income.array().sum():INCOME,output.array().sum():OUTPUT)

This is basically the same as the ideal SQL.
SELECT _id:ID,income.array().sum():INCOME,output.array().sum():OUTPUT FROM computer

The MongoDB script for the same query has the following code scale:

2png

The whole set of query and calculation functions in MongoDB can be implemented with simple SPL script. I will not list them here one by one.

The above example is taken from the following:esProc-driven MongoDB Hackers


It's also easy to embed esProc into Java applicationsplease refer to How to Call an SPL Script in Java

For specific usage, please refer to  Getting started with esProc