Hi there,
The function
func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{},
replacement interface{}, opts ...*options.ReplaceOptions)
(*UpdateResult, error)
silently fails if you pass nil
in the opts parameter.
Passing nothing is OK, passing something nonNil is OK, but nil is not OK.
No possibility to provide a goplayground as it is timeout-ing: timeout running go build
Here is the code I implemented to bypass it:
// ReplaceOne replaces a document
func ReplaceOne(collection string, filter bson.M, newValue interface{}, opts *options.ReplaceOptions) error {
c := MasterSession.Session.Database(DBName).Collection(collection)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if filter == nil {
return ErrInvalidFilter
}
// There is a bug in the driver when options are nil, this is a patch
if opts == nil {
opts = options.Replace().SetUpsert(false)
}
_, err := c.ReplaceOne(ctx, filter, newValue, opts)
return err
}
From a short analysis, this come from
func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{},
replacement interface{}, opts ...*options.ReplaceOptions) (*UpdateResult, error) {
[...]
updateOptions := make([]*options.UpdateOptions, 0, len(opts))
for _, opt := range opts {
uOpts := options.Update()
uOpts.BypassDocumentValidation = opt.BypassDocumentValidation // Erroring line.
uOpts.Collation = opt.Collation
uOpts.Upsert = opt.Upsert
uOpts.Hint = opt.Hint
updateOptions = append(updateOptions, uOpts)
}
return coll.updateOrReplace(ctx, f, r, false, rrOne, false, updateOptions...)
}
Thanks !