blob: 468ccda3538c1a482a4dc9b076d1bda43bb567c9 [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package options
8
9// UpdateOptions represents all possible options to the updateOne() and updateMany() functions
10type UpdateOptions struct {
11 ArrayFilters *ArrayFilters // A set of filters specifying to which array elements an update should apply
12 BypassDocumentValidation *bool // If true, allows the write to opt-out of document level validation
13 Collation *Collation // Specifies a collation
14 Upsert *bool // When true, creates a new document if no document matches the query
15}
16
17// Update returns a pointer to a new UpdateOptions
18func Update() *UpdateOptions {
19 return &UpdateOptions{}
20}
21
22// SetArrayFilters specifies a set of filters specifying to which array elements an update should apply
23// Valid for server versions >= 3.6.
24func (uo *UpdateOptions) SetArrayFilters(af ArrayFilters) *UpdateOptions {
25 uo.ArrayFilters = &af
26 return uo
27}
28
29// SetBypassDocumentValidation allows the write to opt-out of document level validation.
30// Valid for server versions >= 3.2. For servers < 3.2, this option is ignored.
31func (uo *UpdateOptions) SetBypassDocumentValidation(b bool) *UpdateOptions {
32 uo.BypassDocumentValidation = &b
33 return uo
34}
35
36// SetCollation specifies a collation.
37// Valid for server versions >= 3.4.
38func (uo *UpdateOptions) SetCollation(c *Collation) *UpdateOptions {
39 uo.Collation = c
40 return uo
41}
42
43// SetUpsert allows the creation of a new document if not document matches the query
44func (uo *UpdateOptions) SetUpsert(b bool) *UpdateOptions {
45 uo.Upsert = &b
46 return uo
47}
48
49// MergeUpdateOptions combines the argued UpdateOptions into a single UpdateOptions in a last-one-wins fashion
50func MergeUpdateOptions(opts ...*UpdateOptions) *UpdateOptions {
51 uOpts := Update()
52 for _, uo := range opts {
53 if uo == nil {
54 continue
55 }
56 if uo.ArrayFilters != nil {
57 uOpts.ArrayFilters = uo.ArrayFilters
58 }
59 if uo.BypassDocumentValidation != nil {
60 uOpts.BypassDocumentValidation = uo.BypassDocumentValidation
61 }
62 if uo.Collation != nil {
63 uOpts.Collation = uo.Collation
64 }
65 if uo.Upsert != nil {
66 uOpts.Upsert = uo.Upsert
67 }
68 }
69
70 return uOpts
71}