golang 中mgo update报错: The dollar ($) prefixed field '$inc' in '$inc' is not vali

2020-04-08 18:13:01 浏览数 (2)

代码语言:javascript复制
	_, 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.

因此改为:

代码语言:javascript复制
	_, 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
	}

0 人点赞