Golang / mgo – 在更新时设置”$set”条件

huangapple go评论75阅读模式
英文:

Golang / mgo - set "$set" on condition for update

问题

update := bson.M{"$push": bson.M{"versions": Versions{"x", 123}}}

if nctime, _ := time.Parse("2006-01-02 15:04:05", ctime); group.LastUpdate.Before(nctime) {
update["$set"] = bson.M{"lastupdate": nctime}
}

_ = db.Mongo.C("collection").UpdateId(group.Id, update)

我如何将$set部分添加到现有的bson.M中?肯定有一种方法可以做到,但我找不到。

英文:
update := bson.M{"$push": bson.M{"versions" : Versions{"x", 123}}}

if nctime, _ := time.Parse("2006-01-02 15:04:05", ctime); group.LastUpdate.Before(nctime) {
	// update.$set = bson.M{"lastupdate": nctime}
}

_ = db.Mongo.C("collection").UpdateId(group.Id, update)

How can I add the $set part to an existing bson.M? There must be a way to do it, but I couldn't find it.

答案1

得分: 3

update["$set"] = bson.M{"lastupdate": nctime}

英文:
  update["$set"] = bson.M{"lastupdate": nctime}

huangapple
  • 本文由 发表于 2014年9月26日 04:46:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/26047824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定