A few years ago NoSQL might have sounded like a alien monster, but given the advances in the technology and how modern web and mobile applications are storing and accessing data, it is and has become a critical part of today's development.

Couchbase makes it easier than ever to make the switch (or addition) from a relational database to NoSQL with the addition of its latest querying technology.

What You Already Know About SQL

You're probably coming from a relational database management system (RDBMS) such as MySQL, SQL Server, Oracle, or one of the many other popular SQL databases. All these databases share a few common things:

  • Use tables that contain data inside rows and columns
  • Constrained to particular data types (for the most part)

The data in these tables might be related.

Data Relationships Between Tables

If you've been using an RDBMS long enough, you'll know that many of your tables will be related. For example, let's say you have the following two tables:

User Table

Name Type
id int
username varchar
password varchar

Profile Table

Name Type
id int
name varchar
user_id int

A relationship is established between these two tables on the user_id of the profile table and the id of the user table. This is because every user will have a profile. The relationship of course is based on foreign and primary keys.

Joining Table Data

Joining data from two tables is a common practice in an RDBMS. Continuing on the sample I listed above, the following will probably look familiar to you:

You must be asking, where am I going with this, because this is some pretty basic stuff. You'll see in a moment.

What You Think You Know About NoSQL

With Couchbase and NoSQL databases in general your data is stored as JSON. The whole concept of tables and rows is thrown out the window which can feel a little strange at first. For example, what happens if you have the following two JSON documents:

user_1

profile_1

The data in these documents remain unstructured, meaning you are not confined to the schema of a table. If you wanted to add or remove content, you can do so without touching any other documents or changing the allowed structure.

How you would query for these documents would vary between NoSQL platforms, but one thing would remain consistent. If you wanted to join these two documents you would have to make two calls to the database server-side and reconstruct the data into a single document. For example, take this psuedocode:

It isn't very fun having to join documents that way, and it can get quite messy if you have to join more than one document.

How Couchbase Bridges SQL And NoSQL

With Couchbase 4.0 you no longer have to design complicated server-side logic for accomplishing the simple queries you'd typically see in an RDBMS.

Couchbase 4.0 introduces N1QL which can be better thought of as an extension to SQL. What this means for you is that you can use the SQL language that you already know and love, with NoSQL data. Using N1QL you can create the following query for the documents mentioned earlier:

Not so bad right? Sure there are a few differences to what you might be used to in MySQL or similar, but not enough to make it complicated to understand or learn.

So what other advantages does Couchbase's N1QL bring to the world of NoSQL? Many things, but for example, here are a few:

  • All the heavy lifting for getting and manipulating data happens in Couchbase, not your application server.
  • You get all your favorite SQL commands, plus extras.
  • It is fast

So can it be included in the programming languages that you already use?

SQL Works In Just About Every Programming Language, But Will N1QL?

There is a Couchbase SDK for most of the popular programming languages. Whether you're developing in Java, Node.js, or .NET, you can use N1QL in your application just as easy as you can use SQL.

Conclusion

Making the switch from a database that uses SQL to NoSQL can seem like a scary move, but with Couchbase 4.0 and N1QL, the transition between the two platforms is not too different than what you already know. To further get a sense of what the transition would entail, check out this very thorough and interactive N1QL tutorial.

Posted by Nic Raboy, Developer Advocate, Couchbase

Nic Raboy is an advocate of modern web and mobile development technologies. He has experience in Java, JavaScript, Golang and a variety of frameworks such as Angular, NativeScript, and Apache Cordova. Nic writes about his development experiences related to making web and mobile development easier to understand.

Leave a reply