N1QL

This is Couchbase’s query language. With N1QL you can execute SQL queries(SELECT, FROM) and get results from Couchbase DB(which stores data in Json format)

Additional Operators

Data in Couchbase Server is stored in the form of documents, not rows or columns, so db wuold need additional operators to access nested elements.
Operator Description
. Dot operator
[] operator
‘.’ operator is used to refer to children
‘[]’ refers to an element in an array

SELECT children[0].fname
    FROM tutorial
       WHERE fname='Dave'
      
LIKE String matching can be accomplished using the LIKE operator in the WHERE clause.
  % is a wildcard that matches zero or more characters
  _ is a wildcard that matches exactly one character.

// Return info where email ends with yahoo.com
SELECT fname, email
    FROM tutorial 
        WHERE email LIKE '%@yahoo.com'
            
ANY/EVERY - SATISFIES ANY clause allows us to assign a name to an element in the array that we are searching through.
SATISFIES keyword is used to specify the filter condition.

// find any person that has a child over the age of 10
SELECT fname, children
    FROM tutorial 
        WHERE ANY child IN tutorial.children SATISFIES child.age > 10  END
            
AND Combining multiple conditions with AND

// return people having at least one child and having a gmail address
SELECT fname, email, children
    FROM tutorial 
        WHERE ARRAY_LENGTH(children) > 0 AND email LIKE '%@gmail.com'
            

Query examples

Query Result

SELECT *
  FROM tutorial
    WHERE fname = 'Ian'
          

{
  "results": [
    {
      "tutorial": {
        "type": "contact",
        "fname": "Ian",
        "lname": "Taylor",
        "age": 56,
        "email": "ian@gmail.com",
        "children": [
          {
            "fname": "Abama",
            "age": 17,
            "gender": "m"
          }
        ],
        "hobbies": [
          "golf",
          "surfing"
        ]
      }
    }
  ]
}