我们可以在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
}
我收到运行时错误说:位置运算符未找到查询所需的匹配项。未更新的更新:电子邮件。$。已收到
答案 0 :(得分: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)