我们可以在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 回答
它应该是
emails.received
,因为received
不是数组,因此您不需要位置运算符$
: