How to update bin?

How to update bin?

I tried below code to update the value of one of my bin as below -

t := time.Now().UnixNano()
key, err := as.NewKey("fooBar", "users", res.Record.Bins["Email"].(string))
if err != nil {
	result = map[string]string{"error": "Can't create key! Try again " + err.Error()}
	break
}
bin1 := as.NewBin("Password", string(hashPass))
bin2 := as.NewBin("UpdatedAt", t)
err = client.PutBins(nil, key, bin1, bin2)
if err != nil {
	result = map[string]string{"error": "Can't Update Password! Try again" + err.Error()}
	break
}

Doing this actually inserted the row, but I want to only Update existing record.

In the WritePolicy you have a RecordExistsAction with the following possible values:

	// UPDATE means: Create or update record.
	// Merge write command bins with existing bins.
	UPDATE RecordExistsAction = iota

	// UPDATE_ONLY means: Update record only. Fail if record does not exist.
	// Merge write command bins with existing bins.
	UPDATE_ONLY

	// REPLACE means: Create or replace record.
	// Delete existing bins not referenced by write command bins.
	// Supported by Aerospike 2 server versions >= 2.7.5 and
	// Aerospike 3 server versions >= 3.1.6.
	REPLACE

	// REPLACE_ONLY means: Replace record only. Fail if record does not exist.
	// Delete existing bins not referenced by write command bins.
	// Supported by Aerospike 2 server versions >= 2.7.5 and
	// Aerospike 3 server versions >= 3.1.6.
	REPLACE_ONLY

	// CREATE_ONLY means: Create only. Fail if record exists.
	CREATE_ONLY

Do, I need to pass below policy too -

policy := as.NewWritePolicy(0, 0)
policy.RecordExistsAction = "UPDATE"

Nope ^ it doesn’t work. How do I send policy, that it is about UPDATE?

My default values are same as you have provided above.

The values are constants. So you should do the following:

policy := as.NewWritePolicy(0, 0)
policy.RecordExistsAction = as.UPDATE
1 Like

Thanks that worked!

No problem. I’d appreciate it if you could write down your stumbling blocks as a beginner to using our client and server so that we could improve upon our documentation for future users.

Don’t hesitate to ask more questions. Cheers.

The major problem, I faced as a beginner was documentation. It’s not clear and not in systematic order.

Rest, I am still exploring more.

This topic was automatically closed 6 days after the last reply. New replies are no longer allowed.