N1QL query handle output


Gone through a lot of information but can’t find anything about how handle output from n1ql query.

Code taken from couchbase SDK node.js version 2.0/2.1

var N1qlQuery = require('couchbase').N1qlQuery;
var myBucket = myCluster.openBucket('test');
var query = N1qlQuery.fromString('SELECT * FROM test');
myBucket.query(query, function(err, res) {
  if (err) {
    console.log(‘query failed’, err);
  console.log(‘success!’, res);

Result output:

{ test:
{ password: ‘test123’,
username: ‘test’ } } ]

Try 1… Get username

How can i access/get just test.loginUsername without everything else?

Try 2 … Get result to similar json like this…

var jsonDoc = [{
username: ‘test’,
password: ‘test123’

This jsonDoc works when i try printin a table in HTML with angularJS. But when sending result (res) from n1ql query it doesn’t work.

Is there any more information how to handle query/output in node JS etc?

Best Regards

Hey @Johannes,

Have you tried narrowing down your query response instead of returning all document properties in your N1QL query?


var query = N1qlQuery.fromString("SELECT username FROM test");

You’ll always get an array back, but you can also check the size via your application code or loop through it.

You might check the following tutorial I wrote on the Node.js SDK with Couchbase:



Hey nraboy,

The test bucket just got 1 document in it for testing purpose.

It’s not a problem narrow down the query like you said. Just took that code for showing… But i still need to get a single value from the query. Like this, if i just want the username to print in console using node.js… console.log(username ??);

How can i print the value for username only. Without json format?

I have gone through your game api before posting this thread… and it’s not really much help :confused:

Best Regards

Try 2 is fixed… Now i only need to figure out how to print a single value from an n1ql query. Without the json format. =)

Hey @Johannes,

Being that Couchbase is a NoSQL document database, it is going to be pretty difficult to escape from JSON data ;-).

To get the username of a particular document returned it would be something like:


Where x is a particular index in your result array. You will always have an array of JSON data returned from a N1QL query. Each object in the array will represent a particular result that matched your query.

Does this make it any easier?


1 Like

Sorry for late response. But that’s EXACTLY what i was looking for! This should also be added to NodeJS SDK page =)

Many thanks @nraboy !!

No problem! Happy to help :slight_smile:

1 Like