github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/results.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 mongo 8 9 import ( 10 "fmt" 11 12 "go.mongodb.org/mongo-driver/bson" 13 "go.mongodb.org/mongo-driver/bson/primitive" 14 "go.mongodb.org/mongo-driver/x/mongo/driver/operation" 15 ) 16 17 // BulkWriteResult is the result type returned by a BulkWrite operation. 18 type BulkWriteResult struct { 19 // The number of documents inserted. 20 InsertedCount int64 21 22 // The number of documents matched by filters in update and replace operations. 23 MatchedCount int64 24 25 // The number of documents modified by update and replace operations. 26 ModifiedCount int64 27 28 // The number of documents deleted. 29 DeletedCount int64 30 31 // The number of documents upserted by update and replace operations. 32 UpsertedCount int64 33 34 // A map of operation index to the _id of each upserted document. 35 UpsertedIDs map[int64]interface{} 36 } 37 38 // InsertOneResult is the result type returned by an InsertOne operation. 39 type InsertOneResult struct { 40 // The _id of the inserted document. A value generated by the driver will be of type primitive.ObjectID. 41 InsertedID interface{} 42 } 43 44 // InsertManyResult is a result type returned by an InsertMany operation. 45 type InsertManyResult struct { 46 // The _id values of the inserted documents. Values generated by the driver will be of type primitive.ObjectID. 47 InsertedIDs []interface{} 48 } 49 50 // TODO(GODRIVER-2367): Remove the BSON struct tags on DeleteResult. 51 52 // DeleteResult is the result type returned by DeleteOne and DeleteMany operations. 53 type DeleteResult struct { 54 DeletedCount int64 `bson:"n"` // The number of documents deleted. 55 } 56 57 // RewrapManyDataKeyResult is the result of the bulk write operation used to update the key vault collection with 58 // rewrapped data keys. 59 type RewrapManyDataKeyResult struct { 60 *BulkWriteResult 61 } 62 63 // ListDatabasesResult is a result of a ListDatabases operation. 64 type ListDatabasesResult struct { 65 // A slice containing one DatabaseSpecification for each database matched by the operation's filter. 66 Databases []DatabaseSpecification 67 68 // The total size of the database files of the returned databases in bytes. 69 // This will be the sum of the SizeOnDisk field for each specification in Databases. 70 TotalSize int64 71 } 72 73 func newListDatabasesResultFromOperation(res operation.ListDatabasesResult) ListDatabasesResult { 74 var ldr ListDatabasesResult 75 ldr.Databases = make([]DatabaseSpecification, 0, len(res.Databases)) 76 for _, spec := range res.Databases { 77 ldr.Databases = append( 78 ldr.Databases, 79 DatabaseSpecification{Name: spec.Name, SizeOnDisk: spec.SizeOnDisk, Empty: spec.Empty}, 80 ) 81 } 82 ldr.TotalSize = res.TotalSize 83 return ldr 84 } 85 86 // DatabaseSpecification contains information for a database. This type is returned as part of ListDatabasesResult. 87 type DatabaseSpecification struct { 88 Name string // The name of the database. 89 SizeOnDisk int64 // The total size of the database files on disk in bytes. 90 Empty bool // Specfies whether or not the database is empty. 91 } 92 93 // UpdateResult is the result type returned from UpdateOne, UpdateMany, and ReplaceOne operations. 94 type UpdateResult struct { 95 MatchedCount int64 // The number of documents matched by the filter. 96 ModifiedCount int64 // The number of documents modified by the operation. 97 UpsertedCount int64 // The number of documents upserted by the operation. 98 UpsertedID interface{} // The _id field of the upserted document, or nil if no upsert was done. 99 } 100 101 // UnmarshalBSON implements the bson.Unmarshaler interface. 102 // 103 // Deprecated: Unmarshalling an UpdateResult directly from BSON is not supported and may produce 104 // different results compared to running Update* operations directly. 105 func (result *UpdateResult) UnmarshalBSON(b []byte) error { 106 // TODO(GODRIVER-2367): Remove the ability to unmarshal BSON directly to an UpdateResult. 107 elems, err := bson.Raw(b).Elements() 108 if err != nil { 109 return err 110 } 111 112 for _, elem := range elems { 113 switch elem.Key() { 114 case "n": 115 switch elem.Value().Type { 116 case bson.TypeInt32: 117 result.MatchedCount = int64(elem.Value().Int32()) 118 case bson.TypeInt64: 119 result.MatchedCount = elem.Value().Int64() 120 default: 121 return fmt.Errorf("Received invalid type for n, should be Int32 or Int64, received %s", elem.Value().Type) 122 } 123 case "nModified": 124 switch elem.Value().Type { 125 case bson.TypeInt32: 126 result.ModifiedCount = int64(elem.Value().Int32()) 127 case bson.TypeInt64: 128 result.ModifiedCount = elem.Value().Int64() 129 default: 130 return fmt.Errorf("Received invalid type for nModified, should be Int32 or Int64, received %s", elem.Value().Type) 131 } 132 case "upserted": 133 switch elem.Value().Type { 134 case bson.TypeArray: 135 e, err := elem.Value().Array().IndexErr(0) 136 if err != nil { 137 break 138 } 139 if e.Value().Type != bson.TypeEmbeddedDocument { 140 break 141 } 142 var d struct { 143 ID interface{} `bson:"_id"` 144 } 145 err = bson.Unmarshal(e.Value().Document(), &d) 146 if err != nil { 147 return err 148 } 149 result.UpsertedID = d.ID 150 default: 151 return fmt.Errorf("Received invalid type for upserted, should be Array, received %s", elem.Value().Type) 152 } 153 } 154 } 155 156 return nil 157 } 158 159 // IndexSpecification represents an index in a database. This type is returned by the IndexView.ListSpecifications 160 // function and is also used in the CollectionSpecification type. 161 type IndexSpecification struct { 162 // The index name. 163 Name string 164 165 // The namespace for the index. This is a string in the format "databaseName.collectionName". 166 Namespace string 167 168 // The keys specification document for the index. 169 KeysDocument bson.Raw 170 171 // The index version. 172 Version int32 173 174 // The length of time, in seconds, for documents to remain in the collection. The default value is 0, which means 175 // that documents will remain in the collection until they're explicitly deleted or the collection is dropped. 176 ExpireAfterSeconds *int32 177 178 // If true, the index will only reference documents that contain the fields specified in the index. The default is 179 // false. 180 Sparse *bool 181 182 // If true, the collection will not accept insertion or update of documents where the index key value matches an 183 // existing value in the index. The default is false. 184 Unique *bool 185 186 // The clustered index. 187 Clustered *bool 188 } 189 190 var _ bson.Unmarshaler = (*IndexSpecification)(nil) 191 192 type unmarshalIndexSpecification struct { 193 Name string `bson:"name"` 194 Namespace string `bson:"ns"` 195 KeysDocument bson.Raw `bson:"key"` 196 Version int32 `bson:"v"` 197 ExpireAfterSeconds *int32 `bson:"expireAfterSeconds"` 198 Sparse *bool `bson:"sparse"` 199 Unique *bool `bson:"unique"` 200 Clustered *bool `bson:"clustered"` 201 } 202 203 // UnmarshalBSON implements the bson.Unmarshaler interface. 204 // 205 // Deprecated: Unmarshaling an IndexSpecification from BSON will not be supported in Go Driver 2.0. 206 func (i *IndexSpecification) UnmarshalBSON(data []byte) error { 207 var temp unmarshalIndexSpecification 208 if err := bson.Unmarshal(data, &temp); err != nil { 209 return err 210 } 211 212 i.Name = temp.Name 213 i.Namespace = temp.Namespace 214 i.KeysDocument = temp.KeysDocument 215 i.Version = temp.Version 216 i.ExpireAfterSeconds = temp.ExpireAfterSeconds 217 i.Sparse = temp.Sparse 218 i.Unique = temp.Unique 219 i.Clustered = temp.Clustered 220 return nil 221 } 222 223 // CollectionSpecification represents a collection in a database. This type is returned by the 224 // Database.ListCollectionSpecifications function. 225 type CollectionSpecification struct { 226 // The collection name. 227 Name string 228 229 // The type of the collection. This will either be "collection" or "view". 230 Type string 231 232 // Whether or not the collection is readOnly. This will be false for MongoDB versions < 3.4. 233 ReadOnly bool 234 235 // The collection UUID. This field will be nil for MongoDB versions < 3.6. For versions 3.6 and higher, this will 236 // be a primitive.Binary with Subtype 4. 237 UUID *primitive.Binary 238 239 // A document containing the options used to construct the collection. 240 Options bson.Raw 241 242 // An IndexSpecification instance with details about the collection's _id index. This will be nil if the NameOnly 243 // option is used and for MongoDB versions < 3.4. 244 IDIndex *IndexSpecification 245 } 246 247 var _ bson.Unmarshaler = (*CollectionSpecification)(nil) 248 249 // unmarshalCollectionSpecification is used to unmarshal BSON bytes from a listCollections command into a 250 // CollectionSpecification. 251 type unmarshalCollectionSpecification struct { 252 Name string `bson:"name"` 253 Type string `bson:"type"` 254 Info *struct { 255 ReadOnly bool `bson:"readOnly"` 256 UUID *primitive.Binary `bson:"uuid"` 257 } `bson:"info"` 258 Options bson.Raw `bson:"options"` 259 IDIndex *IndexSpecification `bson:"idIndex"` 260 } 261 262 // UnmarshalBSON implements the bson.Unmarshaler interface. 263 // 264 // Deprecated: Unmarshaling a CollectionSpecification from BSON will not be supported in Go Driver 265 // 2.0. 266 func (cs *CollectionSpecification) UnmarshalBSON(data []byte) error { 267 var temp unmarshalCollectionSpecification 268 if err := bson.Unmarshal(data, &temp); err != nil { 269 return err 270 } 271 272 cs.Name = temp.Name 273 cs.Type = temp.Type 274 if cs.Type == "" { 275 // The "type" field is only present on 3.4+ because views were introduced in 3.4, so we implicitly set the 276 // value to "collection" if it's empty. 277 cs.Type = "collection" 278 } 279 if temp.Info != nil { 280 cs.ReadOnly = temp.Info.ReadOnly 281 cs.UUID = temp.Info.UUID 282 } 283 cs.Options = temp.Options 284 cs.IDIndex = temp.IDIndex 285 return nil 286 }