Update specific bin/column and not all in Table/set in Aerospike

query

#1

I have following struct -

type User struct {
    ID string `json:"id"`
    Name string `json:"name"`
    Email string `json:"email"`
    Password string `json:"passwprd"`
    Confirmed int `json:"confirmed"`
    ConfirmCode string `json:"confirmcode"`
    CreatedAt time.Time
    UpdatedAt time.Time
}

Now, Whenever I am inserting data, all is fine, But whenever I update a specific bin Confirmed & ConfirmCode, my all other data is replaced with blank value too.

Here is the code, I am using for an update -

t := time.Now()
u := User{
	Confirmed: 1,
	UpdatedAt: t,
}
key, err := as.NewKey("foobar", "users", "1")
if err != nil {
	ctx.StatusCode(iris.StatusBadRequest)
	ctx.JSON(map[string]string{"error": "Can't update key! Try again " + err.Error()})
	return
}
err = client.PutObject(nil, key, &u)
if err != nil {
	ctx.StatusCode(iris.StatusBadRequest)
	ctx.JSON(map[string]string{"error": "Can't Update object! Try again" + err.Error()})
	return
}

As you can see, While doing Update, I am only providing 2 fields of the struct.

Do I need to provide “Old value” again while doing the update too in Aerospike?


#2

That would be odd for default struct behavior to be replace instead of upsert as with all bin operations… I haven’t touched Go yet but plan to. What’s your client/server version? Does it behave as you expect if using bins instead of struct?


#3

I have modified my code to update all data whenever we need to push even single update.

I am using latest version of aerospike client.


#4

The Go client does not check for zero values, since that would make it unpredictable as to what to do with default values. As a result, all fields are always marshaled and sent to the server.

The correct solution is what you have already done: you need to send all data to the server, or use the Go client’s dedicated API, which is faster and more refined.


#5

what do you mean by “go client dedicated api”? what it does and how is it faster?

Please explain.

Thanks


#6

The *Object class of functions are convenient API meant for beginners. These functions use reflection internally, and are limited in the scope of their functionality.

If you need to use all the capabilities of the Aerospike server, you have to use the other API without the Object in their names. Alternatively, you can build your app via the as_performance build tag and see if it compiles. If it doesn’t, it means you have used a function that uses reflection behind the scenes.


#7

See, It’s always good to point resource or atleast provide some hint to search relevants resources. I am finding hard time understanding your below points -

  1. you have to use the other API without the Object in their names
  2. you can build your app via the as_performance build tag and see if it compiles
  3. The *Object class of functions are convenient API meant for beginners.

In first, I could not understand what object name you are pointing here.

In second, I will check what is as_performance as I was not aware of until now.

In third, which object class, do you mean struct method, I am using or GetObject, PutObject, that is being used. Do you mean to insert, update directing using bin method?


#8

Yes, use the PutBin, GetBin, and other API you can find in the client documentation here:

The policy objects also change the behavior of the server dealing with the data you send to it.


#9

Thanks! that was very helpful.


#10

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