英文:
Mgo pull update not working
问题
我正在尝试使用Go的mgo库实现以下功能:
db.artists.update(
{_id: ObjectId("534944125117082b30000001")},
{
$pull: {
studies: {
_id: ObjectId("53d53591718a522e04000001")
}
}
}
)
这基本上是对artists集合进行更新,我试图根据其id字段从studies数组中移除一个study。
所以在Go中,我使用了以下代码:
pullQuery := &bson.M{"studies": &bson.M{"_id": bson.ObjectIdHex("53d53fd6718a521954000001")}}
err = col.Update(&bson.M{"_id": "534944125117082b30000001"}, &bson.M{"$pull": pullQuery})
但是这似乎不起作用。如果我直接在RoboMongo(mongodb客户端工具)中运行第一个版本,它可以正常工作,但是使用mgo时,它似乎不起作用。它给我返回了错误:"not found"。
谢谢
编辑
以下Go代码已经修改,现在可以正常工作:
err = col.UpdateId(bson.ObjectIdHex("534944125117082b30000001"), &bson.M{"$pull": pullQuery})
英文:
I am trying to achieve the following functionality with mgo library from Go:
db.artists.update(
{_id: ObjectId("534944125117082b30000001")},
{
$pull: {
studies: {
_id: ObjectId("53d53591718a522e04000001")
}
}
})
This is basically an update to artists collection where I am trying to remove a study from studies array, based on it's id field.
So in go I use:
pullQuery := &bson.M{"studies": &bson.M{"_id": bson.ObjectIdHex("53d53fd6718a521954000001")}}
err = col.Update(&bson.M{"_id": "534944125117082b30000001"}, &bson.M{"$pull": pullQuery})
But this doesn't seem to work. If I run the first version directly in RoboMongo (mongodb client utility) it is working fine, but with mgo, it doesn't seem to work. It's giving me the error: "not found".
Thank you
EDIT
The following go code was modified, and it is working just file:
err = col.UpdateId(bson.ObjectIdHex("534944125117082b30000001"), &bson.M{"$pull": pullQuery})
答案1
得分: 3
你第一个$pull
示例中的ObjectId
与Go代码不匹配。你确定你使用了正确的_id
吗?
英文:
The ObjectId
in your first $pull
example does not match the go code. Are you sure you are using the right _id
s?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论