Database Querying with N1QL

SQL for JSON

Get Started Now
Query, Scale, Repeat section

What is N1QL?

N1QL is a declarative query language that extends SQL for JSON. You can query data via native framework and language integration, a fluent API, or the JDBC/ODBC drivers.

N1QL enables you to query JSON documents without any limitations - sort, filter, transform, group, and combine data with a single query. That’s right. You can combine data from multiple documents with a JOIN. That flexible data model you were promised? This is it. You’re no longer limited to “single table” and “table per query” data models.

// Query Statement
SELECT * FROM databases WHERE category='NoSQL'

// Results

{
  "name": "Couchbase Server",
  "version": "4.0",
  "category": "NoSQL",
  "features": [
    {
      "name": "N1QL",
      "capabilities": [
        "JOIN", "NEST", "UNNEST"
      ]
    }
  ]
}

Query with N1QL

// Fluent API
bucket.query(select("name").from("databases").where(x(category).eq(s("NoSQL"))));

// Query Statement
bucket.query(Query.simple("SELECT name FROM databases WHERE category='NoSQL'"));

Querying with N1QL in Java

var query = from db in bucket.Queryable<DataBases>(bucket) where db.category == "NoSQL"
select db.name;

Querying with N1QL in .NET

bucket.query(couchbase.N1qlQuery.fromString("SELECT name FROM databases WHERE category='NoSQL'"));

Querying with N1QL in Node.js

$bucket->query(CouchbaseN1qlQuery::fromString("SELECT name FROM databases WHERE category='NoSQL'"));

Querying with N1QL in PHP

cmd.query = "SELECT name FROM databases WHERE category='NoSQL'";

Querying with N1QL in C/C++

bucket.n1ql_query(N1QLQuery("SELECT name FROM databases WHERE category='NoSQL'"))

Querying with N1QL in Python

You can improve query performance with secondary indexes. Want to create a bunch of indexes? Go for it. Want to create a compound index on multiple fields? Go for it. Worried about what will happen to query performance as the number of nodes increases? Don't. We implemented global secondary indexes. The key word being "global". A global secondary index is not partitioned. It's a whole index. After all, it's a lot faster to check a whole index on one node than it is to check a partial index on every node.

Query, Scale, Repeat section Try It

Try It

Download, explore the reference app, see some code, and run a few queries.