# How to fetch primary key in Aerospike Node.js Client

**URL:** <https://discuss.aerospike.com/t/how-to-fetch-primary-key-in-aerospike-node-js-client/2898>\
**Category:** General Discussion\
**Created:** [May 3, 2016, 7:39pm UTC](https://discuss.aerospike.com/t/how-to-fetch-primary-key-in-aerospike-node-js-client/2898 "2016-05-03T19:39:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Radhika\_Hs](https://avatars.discourse-cdn.com/v4/letter/r/8e7dd6/32.png) [@Radhika\_Hs](https://discuss.aerospike.com/u/Radhika_Hs)\
**Post date:** [May 3, 2016, 7:39pm UTC](https://discuss.aerospike.com/t/how-to-fetch-primary-key-in-aerospike-node-js-client/2898/1 "2016-05-03T19:39:12Z")

</div>

I’m trying to fetch all records along with Primary key from Aerospike. I tried using client.query functionality as below

> var query = client.query(aerospikeDBParams.dbName,“testRecords”);

> var stream = query.execute();

with this I’m getting all the fields except the Primary key.How can i get Primary key along with the other fields?

Thanks in advance!!

---

<div class="post-metadata">

**Author:** ![Jan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.aerospike.com/jan/32/1124_2.png) [@Jan](https://discuss.aerospike.com/u/Jan)\
**Post date:** [May 4, 2016, 9:34am UTC](https://discuss.aerospike.com/t/how-to-fetch-primary-key-in-aerospike-node-js-client/2898/2 "2016-05-04T09:34:04Z")

</div>

Hi @Radhika_Hs, since you posted the identical [question](http://stackoverflow.com/q/36998099/1573115) on Stack Overflow as well, let me just copy @rbotzer’s [answer](http://stackoverflow.com/a/37017675/1573115) here for the benefit of other users of this forum.

You first need to actually store the primary key with the [record](http://www.aerospike.com/docs/architecture/data-model.html#records). The client and server locate the record using its digest, which the client hashes using the (namespace, set, primary-key) information. The default value for the key write policy is `Aerospike.policy.key.DIGEST`. You will need to explicitly set that to `Aerospike.policy.key.SEND` instead.

See the [Aerospike:module](http://www.aerospike.com/apidocs/nodejs/module-aerospike.html) documentation. It contains this example:

```
// global policy, applied to all commands that do not override it
var config = {
  policies: {
    timeout: 100,
    retry: Aerospike.policy.retry.ONCE
  }
}

Aerospike.connect(config, (error, client) => {
  if (error) throw error

  var key = new Aerospike.Key('test', 'demo', 'k1')
  var record = {i: 1234}

  // override policy for put command
  var policy = {
    exists: Aerospike.policy.exists.CREATE,
    key: Aerospike.policy.key.SEND
  }

  client.put(key, record, {}, policy, (error) => {
    if (error && error.code === Aerospike.status.AEROSPIKE_ERR_RECORD_EXISTS) {
      console.info('record already exists')
    } else if (error) {
      throw error
    }
    client.close()
  })
})

```

When the keys are stored with the record a query will return them.

If you are still using the v1 client, you are all set. The `data` event on the record stream returned by the query will include the key object, including the primary key:

```
stream.on('data', (record) => {
  var key = record.key
  console.log('primary key returned: %s', key.key)
})

```

Unfortunately v2 contains a [regression](https://github.com/aerospike/aerospike-client-nodejs/issues/126) in the query functionality and does not currently (as of v2.0.3) return the record key. I will fix this in the next patch release and include the missing key object in the event:

```
stream.on('data', (bins, meta, key) => {
  console.log('primary key returned: %s', key.key)
})

```

Note the difference in the parameters of the `data` event handler in v1 vs v2!
