【问题标题】:How to update a sub-document array fields along with some other fields in Mongodb, Mgo?如何更新子文档数组字段以及 Mongodb、Mgo 中的一些其他字段?
【发布时间】:2016-02-04 11:35:13
【问题描述】:

我们可以更新子文档数组字段以及 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     

}

我收到一个运行时错误消息:位置运算符未从查询中找到所需的匹配项。未扩展的更新:电子邮件.$.received

【问题讨论】:

    标签: mongodb go mgo


    【解决方案1】:

    应该是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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 2017-05-28
      • 1970-01-01
      • 2021-01-28
      • 2013-08-21
      • 1970-01-01
      • 2023-03-05
      相关资源
      最近更新 更多