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

How to update a sub-document array fields along with some other fields in Mongodb, Mgo?

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

}

我收到 运行 时间错误:位置运算符未从查询中找到所需的匹配项。未展开更新:emails.$.received

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