在MGO驱动中更新查询,可以使用bson.M,但无法使用自定义结构。

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

Update query in MGO driver, works with bson.M, but does not work with custome structure

问题

Mgo和Golang问题。

我再次遇到问题。我尝试在数据库中更新记录,但是运行简单的命令visitors.UpdateId(v.Id, bson.M{"$set": zscore}),其中zscoreZscore类型的变量,不起作用。然而,如果我手动将zscore转换为bson.M结构,一切都正常工作。

有人知道如何使用mgo更新mongodb中的记录,而不需要手动将结构值转储到bson.M中吗?

示例:

type Zscore struct {
    a float64 `bson:"a,omitempty" json:"a"`
    b float64 `bson:"b,omitempty" json:"b"`
    c float64 `bson:"c,omitempty" json:"c"`
}

v := Visitor{}
zscore := Zscore{}

visitors := updater.C("visitors")

for result.Next(&v) {
    zscore.a = 1
    zscore.b = 2
    zscore.c = 0

    // 不起作用
    if err := visitors.UpdateId(v.Id, bson.M{"$set": zscore}); err != nil {
        log.Printf("更新访问者时出错:%v\n", err)
    }

    // 起作用
    set := bson.M{
        "zscore.a": zscore.a,
        "zscore.b": zscore.b,
        "zscore.c": zscore.c,
    }

    if err := visitors.UpdateId(v.Id, bson.M{"$set": set}); err != nil {
        log.Printf("更新访问者时出错:%v\n", err)
    }
}
英文:

Mgo and golang question.

I run into problem again. I try to update record in the database, but running simple command visitors.UpdateId(v.Id, bson.M{"$set": zscore}); where zscore is a variable of type Zscore, does not work. However if I manually convert zscore to bson.M structure, everything works fine.

Does anybody know how to update the record in mongodb using mgo, without manually dumping structure values into bson.M?

Example:

type Zscore struct {
    a float64 `bson:"a,omitempty" json:"a"`
    b float64 `bson:"b,omitempty" json:"b"`
    c float64 `bson:"c,omitempty" json:"c"`
}

v := Visitor{}
zscore := Zscore{}

visitors := updater.C("visitors")

for result.Next(&v) {
    zscore.a = 1
    zscore.b = 2
    zscore.c = 0

    //does not work
    if err := visitors.UpdateId(v.Id, bson.M{"$set": zscore}); err != nil    {
			log.Printf("Got error while updating visitor: %v\n", err)
    }

    //works
    set := bson.M{
	    "zscore.a": zscore.a,
	    "zscore.b": zscore.b,
	    "zscore.c": zscore.c,
    }

    if err := visitors.UpdateId(v.Id, bson.M{"$set": set}); err != nil {
	    log.Printf("Got error while updating visitor: %v\n", err)
    }
}

答案1

得分: 1

我知道的所有Go编组包,包括bson包,在编组私有字段(以小写字母开头)时都不会进行编组。要解决此问题,只需通过将它们的名称的第一个字母大写来导出相关字段。

此外,请注意,除了上述问题之外,你的示例的第一部分无法以与第二部分等效的方式进行编组。bson.M{"$set": zscore} 等效于 bson.M{"$set": bson.M{"a": ... 等等 ...}}

英文:

All Go marshaling packages I'm aware of, including the bson package, will not marshal fields that are private (start with a lowercase letter). To fix the issue, just export the relevant fields by uppercasing the first letter of their name.

Also note that, besides the issue mentioned above, the first part of your example will not marshal in an equivalent way to the second part. bson.M{"$set": zscore} is equivalent to bson.M{"$set": bson.M{"a": ... etc ...}}.

huangapple
  • 本文由 发表于 2015年3月9日 23:51:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/28946273.html
匿名

发表评论

匿名网友

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

确定