skip to content
Alvin Lucillo

Go MongoDB - No updated document

/ 1 min read

💻 Tech

If your update operation like UpdateOne doesn’t take effect, check UpdateResult object returned from the function. If the count variables like MatchedCount are 0, one of the reasons, apart from the obvious ones like non-existing document, is that the id used in the filter is not of type primitive.ObjectID. Probably, you used a string for _id.

type Personne struct {
	ID primitive.ObjectID `bson:"_id"`
}

// ... some code

var personnes []Personne
for cursor.Next(ctx) {
	var p Personne
	err := cursor.Decode(&p)
	if err != nil {
		return fmt.Errorf("failed to decode document: %v", err)
	}
	personnes = append(personnes, p)
}

// ... some code

result, err :=col.UpdateOne(context.TODO(), filter, personne)