如何在Mgo的Mongodb中更新子文档数组字段以及其他一些字段?
问题描述:
Can we update sub-document array fields along with other document fields in Mgo? If so, please help me with my query.
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)
My Database Structs are as follows:
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
}
I am getting a run time error saying: The positional operator did not find the match needed from the query. Unexpanded update: emails.$.received
我们可以更新子文档数组字段以及Mgo中的其他文档字段吗? 如果可以,请帮助我 我的查询。 p>
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}} \ 错误:= c.Update(colQuerier,change)
code> pre>
我的数据库结构如下: p>
类型电子邮件struct {
Id bson.ObjectId`bson:“ _ id,omitempty”`
收到的字符串
发送字符串
}
type用户结构{
Id bson.ObjectId`bson:“ _ id,omitempty “`
电子邮件字符串
密码字符串
放置字符串
电子邮件
}
code> pre>
我遇到了运行时错误,说: 运算符未从查询中找到所需的匹配项。 未扩展的更新:已收到电子邮件。$。 p>
div>
答
It should be emails.received
as received
is not an array, so you don't need the positional operator $
:
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)