Get document _id from N1QL query row

I’m using a bucket with SyncGateway but I need to run a script on the server and for this I’m using nodeJS. For this script I run some N1QL queries but for one of these I need to retrieve the document “_id” attribute, is it possible?

Here is my code:

var couchbase = require('couchbase-promises');
var cluster = new couchbase.Cluster('couchbase://localhost/');
var bucket = cluster.openBucket(METADATA_BUCKET);
var N1qlQuery = couchbase.N1qlQuery;

var allTableDefinitionsQuery = N1qlQuery.fromString('SELECT * FROM METADATA WHERE type = $1');
var allTableDefinitionsReq = metadataBucket.query(allTableDefinitionsQuery, ['TABLE_DEFINITION']);

allTableDefinitionsReq.on('row', async function(row) {
  console.log(row);
  //Some stuff
})

allTableDefinitionsReq.on('error', function(err) {
  console.error('Got error %j', err);
})

allTableDefinitionsReq.on('end', function(meta) {
  console.log('All table definitions received.', meta);
})

And when my row is logged here is what I got:

{ 
    METADATA: {
	    _sync: { 
	        history: [Object],
		    recent_sequences: [Object],
		    rev: '1-3523bb8724b0e74c11786af9757d794f',
		    sequence: 226,
		    time_saved: '2017-05-17T15:08:48.230225691+02:00' 
	    },
	    headers: [ [Object] ],
	    name: 'Table A',
	    rows: [ [Object], [Object], [Object], [Object] ],
	    type: 'TABLE_DEFINITION'
    }
}

but their is no “_id” property here, how could I get it?

you should get document “_id” by META().id function

for example

SELECT META(METADATA).id,METADATA.* FROM METADATA WHERE type = $1
1 Like