github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/collectionoptions.go (about) 1 // 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 7 package options 8 9 import ( 10 "go.mongodb.org/mongo-driver/bson/bsoncodec" 11 "go.mongodb.org/mongo-driver/mongo/readconcern" 12 "go.mongodb.org/mongo-driver/mongo/readpref" 13 "go.mongodb.org/mongo-driver/mongo/writeconcern" 14 ) 15 16 // CollectionOptions represents options that can be used to configure a Collection. 17 type CollectionOptions struct { 18 // ReadConcern is the read concern to use for operations executed on the Collection. The default value is nil, which means that 19 // the read concern of the Database used to configure the Collection will be used. 20 ReadConcern *readconcern.ReadConcern 21 22 // WriteConcern is the write concern to use for operations executed on the Collection. The default value is nil, which means that 23 // the write concern of the Database used to configure the Collection will be used. 24 WriteConcern *writeconcern.WriteConcern 25 26 // ReadPreference is the read preference to use for operations executed on the Collection. The default value is nil, which means that 27 // the read preference of the Database used to configure the Collection will be used. 28 ReadPreference *readpref.ReadPref 29 30 // BSONOptions configures optional BSON marshaling and unmarshaling 31 // behavior. 32 BSONOptions *BSONOptions 33 34 // Registry is the BSON registry to marshal and unmarshal documents for operations executed on the Collection. The default value 35 // is nil, which means that the registry of the Database used to configure the Collection will be used. 36 Registry *bsoncodec.Registry 37 } 38 39 // Collection creates a new CollectionOptions instance. 40 func Collection() *CollectionOptions { 41 return &CollectionOptions{} 42 } 43 44 // SetReadConcern sets the value for the ReadConcern field. 45 func (c *CollectionOptions) SetReadConcern(rc *readconcern.ReadConcern) *CollectionOptions { 46 c.ReadConcern = rc 47 return c 48 } 49 50 // SetWriteConcern sets the value for the WriteConcern field. 51 func (c *CollectionOptions) SetWriteConcern(wc *writeconcern.WriteConcern) *CollectionOptions { 52 c.WriteConcern = wc 53 return c 54 } 55 56 // SetReadPreference sets the value for the ReadPreference field. 57 func (c *CollectionOptions) SetReadPreference(rp *readpref.ReadPref) *CollectionOptions { 58 c.ReadPreference = rp 59 return c 60 } 61 62 // SetBSONOptions configures optional BSON marshaling and unmarshaling behavior. 63 func (c *CollectionOptions) SetBSONOptions(opts *BSONOptions) *CollectionOptions { 64 c.BSONOptions = opts 65 return c 66 } 67 68 // SetRegistry sets the value for the Registry field. 69 func (c *CollectionOptions) SetRegistry(r *bsoncodec.Registry) *CollectionOptions { 70 c.Registry = r 71 return c 72 } 73 74 // MergeCollectionOptions combines the given CollectionOptions instances into a single *CollectionOptions in a 75 // last-one-wins fashion. 76 // 77 // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a 78 // single options struct instead. 79 func MergeCollectionOptions(opts ...*CollectionOptions) *CollectionOptions { 80 c := Collection() 81 82 for _, opt := range opts { 83 if opt == nil { 84 continue 85 } 86 if opt.ReadConcern != nil { 87 c.ReadConcern = opt.ReadConcern 88 } 89 if opt.WriteConcern != nil { 90 c.WriteConcern = opt.WriteConcern 91 } 92 if opt.ReadPreference != nil { 93 c.ReadPreference = opt.ReadPreference 94 } 95 if opt.Registry != nil { 96 c.Registry = opt.Registry 97 } 98 } 99 100 return c 101 }