globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

Updates #159

Closed ignishub closed 6 years ago

ignishub commented 6 years ago

Sorry for probably stupid question but:

I have struct like:

type Page struct {
    ID      bson.ObjectId `bson:"_id,omitempty"`
    URL     string        `bson:"url,omitempty"` // Модель для определения создания
    Title   string        `bson:"title"`
    Content template.HTML `bson:"content"`
}

I inserted this struct in DB. In Mongo i have index that prevent from duplicationg URL I am trying to update Page

func PageUpdate(oldurl string, page *Page) error {
    page.Model.Update()
    if page.URL == oldurl {
        page.URL = ""
    }
    return PageCollection.Update(bson.M{"url": oldurl}, page)
}

BUT. When i trying to change url in Mongo i still have OLD page AND new page with updated data.

Is that normal?

szank commented 6 years ago

Hi @ignishub. Just to confirm: You have created unique index on the field url?

ignishub commented 6 years ago

Yes

szank commented 6 years ago

Hi, could you please provide more data? Can you reproduce the same condition in the mongo shell? Could you please paste the index definition?

mgo behaviour can be mapped to the mongo shell commands, so I would suggest that you try to reproduce your problem in the mongo shell to verify it is a bug in mgo.

I had a quick glance at the update documentation and found this: https://docs.mongodb.com/manual/reference/method/db.collection.update/#use-unique-indexes

Maybe using upsert will solve your issue.

If you have the _id field available, you could use it as the query parameter. Also are you sure the _id field is not modified after retrieval?

ignishub commented 6 years ago

Sorry for being inactive. I will try that when i'll be near to my pc. Thank you!

domodwyer commented 6 years ago

Hi @ignishub

I'm going to close this, but feel free to reply.

Dom

ignishub commented 6 years ago

Sorry for being so long. I solved this problem. This was a bug in my code. Sorry =(