DEV Community

Cover image for JS&Friends Conf: Querying NoSQL with SQL - Matthew Groves
Adam Romig 🇵🇭
Adam Romig 🇵🇭

Posted on

JS&Friends Conf: Querying NoSQL with SQL - Matthew Groves

Querying NoSQL with SQL - Matthew Groves

Matthew Groves

I had always wanted to read up more on NoSQL databases and how they differ from relational databases. I have worked with SQL for 14 years so the relational design & structure is strong in how I think of table schema.

Matthew explained the benefits of using NoSQL over SQL: scalability (spin up more servers or cluster them if needed), flexiblity (no strict schema needed), and performance. He noted that we don't have to totally abandon relational databases - they can coexist.

While relational databases link separate tables by foreign keys, the JSON document model (the format NoSQL uses) can place related data in the same document as the main record. These extra pieces might be array objects if it contains more than one instance of child data. Other documents for seperate concepts can be created and referenced by a key.

Once he explained the concepts behind NoSQL, he showed us that queries (very similar to SQL's) can be created to find data. This is called N1QL (pronounced nickel) which is a Couchbase-centric query language. Azure Cosmos has something similar as well. Other NoSQL platforms would need to use aggregate functions which, to me, don't look very readable at first glance.

SELECT field
FROM `bucket` /* buckets contain logical groups of data-items */
WHERE otherfield = somevalue
/* JOINs are possible too! */
JOIN `otherbucket` ON ( = META(otherbucket).id)
LIMIT number;
Enter fullscreen mode Exit fullscreen mode

N1QL queries return JSON objects that we can parse in our native coding language.

If there's anything to take away from this talk, Matthew says it's these 3 things:

  • Pick the right application. Whether it's NoSQL or SQL, use what makes sense.
  • JSON data is modeled differently. The structure of a document may be very different than a table.
  • Query NoSQL data with SQL. So much easier to write.

← Back to main JS&Friends article

Top comments (0)