_, err := c.Find(bson.M{"index": index}).Apply(mgo.Change{ Upsert: true, ReturnNew: false, Update: bson.M{"create_time": time.Now(), "update_time": time.Now(), "operation": 0, "$inc": bson.M{"req_cnt": 1}}, }, nil) if err != nil { return err }
由于"$inc"和前面的update冲突,报错误:The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.
因此改为:
_, err := c.Find(bson.M{"index": index}).Apply(mgo.Change{ Upsert: true, ReturnNew: false, Update: bson.M{"$set": bson.M{"create_time": time.Now(), "update_time": time.Now(), "operation": 0}, "$inc": bson.M{"req_cnt": 1}}, }, nil) if err != nil { return err }