Mgo拉取更新不起作用。

huangapple go评论86阅读模式
英文:

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 _ids?

huangapple
  • 本文由 发表于 2014年7月28日 02:12:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/24984029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定