I want something like this:
- find one record in mongo db
old_record
- update this record to
new_record
- return
old_record
I write code like this:
ret = nil
// First, Find the obj
obj := &orm.QuerySetObj{}
err2 := this.querySetCollection.With(session).Find(objKey).One(obj)
if nil != err2 {
this.logger.Println("Error find obj")
return
}
ret = obj
// Then, update this obj
obj.updateTime = time.Now().Unix()
err3 := this.querySetCollection.With(session).Upsert(objKey, obj)
if nil != err3 {
this.logger.Println("Error update obj")
return
}
return
but, I think find
and update
should be an atomic
operation, so my code is not safe.
how can I do this in an atomic operation