Blog Post

Announcing Couchbase Query Language (N1QL) - Developer Preview 2!

Ilam Siva of Couchbase Published

I am very excited to announce the latest Developer Preview release of the Couchbase Query Language (N1QL).

So, what's the excitement? What's this N1QL all about?

N1QL is pronounced "nickel". It is our next generation query language. N1QL presents easy and familiar abstractions to quickly develop scalable applications that work with next generation database systems. It allows for filter expressions, aggregate expressions and many other features to build a rich application.

A couple of months ago we announced the first developer preview at CouchConf 2013 [SF] and now we are announcing the second developer preview.

Since N1QL is quite new, let me give you a quick overview of it. Rather than rehash the language details which is abundantly explained in our Documentation, I think it is more valuable for me to use the blog space to try to give you a very quick flavor of the language, some of the thought process that went into making design decisions, why we are doing developer previews and what we are thinking about for the future. I will also provide links for additional resources if you want more details on the developer preview.

N1QL is a query language designed to work with semi-structured and unstructured data. In its current avatar, we fully support the widely used JSON format, and support for additional formats could come in the future. At the moment, the query engine runs outside of Couchbase so that we can quickly iterate over changes and push out DP releases. But note that N1QL will be tightly integrated with the Couchbase Server.  We want to solicit feedback on the language and learn about the things that you would like to see added or changed.

Here is an example N1QL query:

SELECT fname, email, children

FROM contacts

WHERE LENGTH(children) > 0 AND email LIKE '%@gmail.com'

"Oh! Wait a minute, I thought you said N1QL…Did I just see SQL there?"

Well, you are not wrong! We have intentionally modeled N1QL to have a SQL-like syntax. However we go beyond SQL as well. We fully support path expressions for accessing nested objects in JSON and there are other additional clauses for NoSQL support, such as checking if an attribute is “MISSING” and differentiating that from an attribute that exists but with a “NULL” value. These are two different things, and very useful to be able to query knowing that you can differentiate one from another.

And initial feedback from the community has been very favorable to this decision of modeling it on SQL-like syntax. There are a lot of developers who are very familiar with SQL for relational databases, and we want to provide them an easier path (pun intended smiley) to transition to querying in the NoSQL world.

And here is another example – this time showing a path expression:

SELECT children[0].fname AS cname

FROM contacts

WHERE fname = 'Dave'

Here children is an embedded array in the JSON document and the dot notation is used to access the attribute called fname of the first element in the children array.

Another example – sometimes you want the filter to be applied to arrays nested inside a document:

SELECT fname, children

FROM contacts

WHERE ANY child.age > 10 OVER child IN tutorial.children END

In this example, we want to find any person that has a child over the age of 10.

New in DP2:

In Developer Preview 2, we have also added built-ins for DATE/TIME handling, and we have made performance improvements through better usage of indexes. And certainly we have made a number of bug fixes as well to improve stability of the query engine. You can look for further details in the Release Notes for DP2.

What's next for N1QL?

We are looking at features like subqueries, JOINs and more importantly we understand that we need a robust and highly performing back end. To meet these goals, we are also working on secondary indexes and researching data structures that will help us optimize our querying and indexing. Stay tuned for more announcements in the future as we continue to make headway in all these areas.

Alright! I hear people say, “I am excited, but where do I turn to learn more about all these exciting things?”

Bookmark:  http://query.couchbase.com

That will be your one-stop shop for all the latest information on query features for Couchbase.

And there is also more comprehensive documentation at that website that you can use as a reference.

Also look at Robin's blog post on N1QL

So, go ahead, download N1QL, take it for a spin! And be sure to send us your feedback via our communities portal.