0

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})
eAbi
  • 3,220
  • 4
  • 25
  • 39

1 Answers1

3

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

Justin Case
  • 1,503
  • 11
  • 20
  • Oh I hate when those kinds of things are happening. Yes, I forgot to use the bson.ObjectIdHex function when querying the document. Thank you! – eAbi Jul 28 '14 at 08:56