You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

results.go 4.0 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. package mongo
  7. import (
  8. "fmt"
  9. "go.mongodb.org/mongo-driver/bson"
  10. "go.mongodb.org/mongo-driver/x/mongo/driver/operation"
  11. )
  12. // BulkWriteResult holds the result of a bulk write operation.
  13. type BulkWriteResult struct {
  14. InsertedCount int64
  15. MatchedCount int64
  16. ModifiedCount int64
  17. DeletedCount int64
  18. UpsertedCount int64
  19. UpsertedIDs map[int64]interface{}
  20. }
  21. // InsertOneResult is a result of an InsertOne operation.
  22. //
  23. // InsertedID will be a Go type that corresponds to a BSON type.
  24. type InsertOneResult struct {
  25. // The identifier that was inserted.
  26. InsertedID interface{}
  27. }
  28. // InsertManyResult is a result of an InsertMany operation.
  29. type InsertManyResult struct {
  30. // Maps the indexes of inserted documents to their _id fields.
  31. InsertedIDs []interface{}
  32. }
  33. // DeleteResult is a result of an DeleteOne operation.
  34. type DeleteResult struct {
  35. // The number of documents that were deleted.
  36. DeletedCount int64 `bson:"n"`
  37. }
  38. // ListDatabasesResult is a result of a ListDatabases operation. Each specification
  39. // is a description of the datbases on the server.
  40. type ListDatabasesResult struct {
  41. Databases []DatabaseSpecification
  42. TotalSize int64
  43. }
  44. func newListDatabasesResultFromOperation(res operation.ListDatabasesResult) ListDatabasesResult {
  45. var ldr ListDatabasesResult
  46. ldr.Databases = make([]DatabaseSpecification, 0, len(res.Databases))
  47. for _, spec := range res.Databases {
  48. ldr.Databases = append(
  49. ldr.Databases,
  50. DatabaseSpecification{Name: spec.Name, SizeOnDisk: spec.SizeOnDisk, Empty: spec.Empty},
  51. )
  52. }
  53. ldr.TotalSize = res.TotalSize
  54. return ldr
  55. }
  56. // DatabaseSpecification is the information for a single database returned
  57. // from a ListDatabases operation.
  58. type DatabaseSpecification struct {
  59. Name string
  60. SizeOnDisk int64
  61. Empty bool
  62. }
  63. // UpdateResult is a result of an update operation.
  64. //
  65. // UpsertedID will be a Go type that corresponds to a BSON type.
  66. type UpdateResult struct {
  67. // The number of documents that matched the filter.
  68. MatchedCount int64
  69. // The number of documents that were modified.
  70. ModifiedCount int64
  71. // The number of documents that were upserted.
  72. UpsertedCount int64
  73. // The identifier of the inserted document if an upsert took place.
  74. UpsertedID interface{}
  75. }
  76. // UnmarshalBSON implements the bson.Unmarshaler interface.
  77. func (result *UpdateResult) UnmarshalBSON(b []byte) error {
  78. elems, err := bson.Raw(b).Elements()
  79. if err != nil {
  80. return err
  81. }
  82. for _, elem := range elems {
  83. switch elem.Key() {
  84. case "n":
  85. switch elem.Value().Type {
  86. case bson.TypeInt32:
  87. result.MatchedCount = int64(elem.Value().Int32())
  88. case bson.TypeInt64:
  89. result.MatchedCount = elem.Value().Int64()
  90. default:
  91. return fmt.Errorf("Received invalid type for n, should be Int32 or Int64, received %s", elem.Value().Type)
  92. }
  93. case "nModified":
  94. switch elem.Value().Type {
  95. case bson.TypeInt32:
  96. result.ModifiedCount = int64(elem.Value().Int32())
  97. case bson.TypeInt64:
  98. result.ModifiedCount = elem.Value().Int64()
  99. default:
  100. return fmt.Errorf("Received invalid type for nModified, should be Int32 or Int64, received %s", elem.Value().Type)
  101. }
  102. case "upserted":
  103. switch elem.Value().Type {
  104. case bson.TypeArray:
  105. e, err := elem.Value().Array().IndexErr(0)
  106. if err != nil {
  107. break
  108. }
  109. if e.Value().Type != bson.TypeEmbeddedDocument {
  110. break
  111. }
  112. var d struct {
  113. ID interface{} `bson:"_id"`
  114. }
  115. err = bson.Unmarshal(e.Value().Document(), &d)
  116. if err != nil {
  117. return err
  118. }
  119. result.UpsertedID = d.ID
  120. default:
  121. return fmt.Errorf("Received invalid type for upserted, should be Array, received %s", elem.Value().Type)
  122. }
  123. }
  124. }
  125. return nil
  126. }