首页 文章

如何在Mongodb,Mgo中更新子文档数组字段以及其他一些字段?

提问于
浏览
3

我们可以在Mgo中更新子文档数组字段以及其他文档字段吗?如果是这样,请帮助我查询 .

c := db.C("user")
 colQuerier := bson.M{"email": *olduname}
 change := bson.M{"$set":bson.M{"password":*pwd, "place":*place, "emails.$.received":*received,"emails.$.sent":*sent}}
        err := c.Update(colQuerier, change)

我的数据库结构如下:

type Emails struct{
    Id          bson.ObjectId `bson:"_id,omitempty"`
    Received string
    Sent    string  
}

type User   struct {
    Id          bson.ObjectId `bson:"_id,omitempty"`
    Email       string
    Password    string
    Place       string
    Emails     

}

我收到运行时错误说:位置运算符没有找到查询所需的匹配项 . 未更新的更新:电子邮件 . $ . 已收到

1 回答

  • 2

    它应该是 emails.received ,因为 received 不是数组,因此您不需要位置运算符 $

    c := db.C("user")
    colQuerier := bson.M{"email": *olduname}
    change := bson.M{"$set":bson.M{"password":*pwd, "place":*place, "emails.received":*received}}
    err := c.Update(colQuerier, change)
    

相关问题