Search code examples
mongodbgomgo

Mgo pull update not working


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})

Solution

  • The ObjectId in your first $pull example does not match the go code. Are you sure you are using the right _ids?