5

I'm trying to retrieve the Primary key in the Aerospike node.js client using client.get(). I have inserted the records using client.put() by changing the policy to Aerospike.policy.key.SEND as mentioned here and here.

Now I want to retrieve the records along with the Primary key. I tried doing it like this as mentioned in the Aerospike Documentation but it doesn't seem to work.

var key = aerospike.key(aerospikeDBParams.defaultNamespace,aerospikeDBParams.defaultSet,count);
    var readpolicy = {
        key: aerospike.policy.key.SEND
    }
    client.get(key, readpolicy, function(err, rec, meta){}

I get all the bins but not the Primary key. Am I missing something here?

Thanks in advance.

Community
  • 1
  • 1
Abhijith S
  • 201
  • 1
  • 2
  • 7
  • 2
    Hi Abhijith, if you look at the API documentation for the [Client#get](http://www.aerospike.com/apidocs/nodejs/Client.html#get__anchor) call you can see that the [Client~recordCallback](http://www.aerospike.com/apidocs/nodejs/Client.html#~recordCallback) callback function for this database operation actually has 4 parameters: function (error, record, metadata, key). – Jan Hecking May 07 '16 at 09:04

2 Answers2

5

The fourth paramater of function is information about primary key you want.

Take my code as example:

  var readpolicy = {
      key: Aerospike.policy.key.SEND
  }
  var key = new Aerospike.Key(ns, set, "sel-fish")
  client.get(key, readpolicy, function (err, record, metadata, key) {
    if (null == err) {
      console.log("get ok")
      console.log(record)
      console.log(metadata)
      console.log(key)
    }
  })

The output is :

get ok
{ uid: 1000,
  name: 'sel-fish',
  dob: { mm: 12, dd: 29, yy: 1995 },
  friends: [ 1001, 1002, 1003 ],
  avatar: <Buffer 0a 0b 0c> }
{ ttl: 431997, gen: 3 }
Key { ns: 'test', set: '14', key: 'sel-fish', digest: null }

The version of driver is aerospike@2.0.3

sel-fish
  • 4,308
  • 2
  • 20
  • 39
2

You're looking at the documentation of the older 1.x client. The documentation for the 2.x client is at http://www.aerospike.com/apidocs/nodejs/

As long as the key is stored in the write operation, you should be able to get it with subsequent reads.

const Aerospike = require('aerospike')
function assertOk (error, message) {
  if (error) {
    console.error('ERROR - %s: %s [%s]\n%s', message, error.message, error.code, error.stack)
    throw error
  }
}

const Key = Aerospike.Key

Aerospike.connect({ hosts: '127.0.0.1:3000' }, function (error, client) {
  assertOk(error, 'Connecting to Aerospike cluster')

  var key = new Aerospike.Key('test', 'demo', 1)
  var bins = { a: 1, b: 2 }
  var policy = {
    key: Aerospike.policy.key.SEND
  }

  client.put(key, bins, {}, policy, function (error) {
    assertOk(error, 'Writing database record')

    client.get(key, function (error, record, meta) {
      assertOk(error, 'Reading database record')

      console.log(record, meta)
    })
  })
})
Ronen Botzer
  • 6,951
  • 22
  • 41
  • 1
    Thanks Ronen, but It still doesn't work for me. Not sure what I'm missing. Also, when I run a query in AQL for the set like Select * from test.test I get the key and all the bins. However, when I try something like `select * from test.test where key=1`, it says "0 Records in Set" I can retrieve the record using `select * from test.test where PK=1` Is this normal behavior? – Abhijith S May 06 '16 at 09:05
  • 1
    PK is the reserved word you need. You can use `help` to see AQL's syntax. Which version of the node client are you using? – Ronen Botzer May 06 '16 at 13:56
  • 2
    Actually the docs at [www.aerospike.com/docs/client/nodejs/](http://www.aerospike.com/docs/client/nodejs/) have been updated to cover client version 2 as well. But they focus more on general concepts and usage; for detailed information about specific API calls it is indeed better to consult the new API docs at [www.aerospike.com/apidocs/nodejs/](http://www.aerospike.com/apidocs/nodejs/). – Jan Hecking May 07 '16 at 09:01