Abhijith S
Abhijith S

Reputation: 211

Retrieve Primary Key from Aerospike node.js client

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.

Upvotes: 5

Views: 461

Answers (2)

Ronen Botzer
Ronen Botzer

Reputation: 7117

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)
    })
  })
})

Upvotes: 2

sel-fish
sel-fish

Reputation: 4486

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 [email protected]

Upvotes: 5

Related Questions