Add UpdateSet, Relaxed in client, Fix tests

This commit is contained in:
George Suntres
2026-04-22 10:22:23 -04:00
parent 45f9ac558f
commit 188c5a1be1
9 changed files with 163 additions and 31 deletions

36
update_set.go Normal file
View File

@@ -0,0 +1,36 @@
package mongo
import (
"context"
"go.mongodb.org/mongo-driver/v2/bson"
)
// UpdateSet search documents using filter and updates the first it finds using the $set operator.
func (c *MongoClient) UpdateSet(ctx context.Context, database, name string, filter, data bson.M) (bool, error) {
collection := c.GetCollection(database, name)
prepareForUpdateSet(data)
if err := c.DiscriminatorCheckAndApplyToFilter(ctx, name, filter); err != nil {
return false, err
}
if err := c.DiscriminatorOmitInData(name, data); err != nil {
return false, err
}
update := bson.M{ "$set": data }
updateResult, err := collection.UpdateOne(ctx, filter, update)
if err != nil {
return false, err
}
changed := updateResult.ModifiedCount != 0
return changed, nil
}
func prepareForUpdateSet(data bson.M) {
ensureUpdatedAt(data)
}