/src/mongo/gotools/src/github.com/mongodb/mongo-tools/vendor/go.mongodb.org/mongo-driver/bson/doc.go

https://github.com/paralect/mongo · Go · 116 lines · 1 code · 1 blank · 114 comment · 0 complexity · 140b5dd3400c799f2212ffa4d9a84996 MD5 · raw file

  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 bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization format used to
  7. // store documents and make remote procedure calls in MongoDB. The BSON specification is located at https://bsonspec.org.
  8. //
  9. // Raw BSON
  10. //
  11. // The Raw family of types is used to validate and retrieve elements from a slice of bytes. This
  12. // type is most useful when you want do lookups on BSON bytes without unmarshaling it into another
  13. // type.
  14. //
  15. // Example:
  16. // var raw bson.Raw = ... // bytes from somewhere
  17. // err := raw.Validate()
  18. // if err != nil { return err }
  19. // val := raw.Lookup("foo")
  20. // i32, ok := val.Int32OK()
  21. // // do something with i32...
  22. //
  23. // Native Go Types
  24. //
  25. // The D and M types defined in this package can be used to build representations of BSON using native Go types. D is a
  26. // slice and M is a map. For more information about the use cases for these types, see the documentation on the type
  27. // definitions.
  28. //
  29. // Example:
  30. // bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
  31. // bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
  32. //
  33. // When decoding BSON to a D or M, the following type mappings apply when unmarshalling:
  34. //
  35. // 1. BSON int32 unmarshals to an int32.
  36. // 2. BSON int64 unmarshals to an int64.
  37. // 3. BSON double unmarshals to a float64.
  38. // 4. BSON string unmarshals to a string.
  39. // 5. BSON boolean unmarshals to a bool.
  40. // 6. BSON embedded document unmarshals to the parent type (i.e. D for a D, M for an M).
  41. // 7. BSON array unmarshals to a bson.A.
  42. // 8. BSON ObjectId unmarshals to a primitive.ObjectID.
  43. // 9. BSON datetime unmarshals to a primitive.Datetime.
  44. // 10. BSON binary unmarshals to a primitive.Binary.
  45. // 11. BSON regular expression unmarshals to a primitive.Regex.
  46. // 12. BSON JavaScript unmarshals to a primitive.JavaScript.
  47. // 13. BSON code with scope unmarshals to a primitive.CodeWithScope.
  48. // 14. BSON timestamp unmarshals to an primitive.Timestamp.
  49. // 15. BSON 128-bit decimal unmarshals to an primitive.Decimal128.
  50. // 16. BSON min key unmarshals to an primitive.MinKey.
  51. // 17. BSON max key unmarshals to an primitive.MaxKey.
  52. // 18. BSON undefined unmarshals to a primitive.Undefined.
  53. // 19. BSON null unmarshals to a primitive.Null.
  54. // 20. BSON DBPointer unmarshals to a primitive.DBPointer.
  55. // 21. BSON symbol unmarshals to a primitive.Symbol.
  56. //
  57. // The above mappings also apply when marshalling a D or M to BSON. Some other useful marshalling mappings are:
  58. //
  59. // 1. time.Time marshals to a BSON datetime.
  60. // 2. int8, int16, and int32 marshal to a BSON int32.
  61. // 3. int marshals to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and a BSON int64
  62. // otherwise.
  63. // 4. int64 marshals to BSON int64.
  64. // 5. uint8 and uint16 marshal to a BSON int32.
  65. // 6. uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32,
  66. // inclusive, and BSON int64 otherwise.
  67. //
  68. // Structs
  69. //
  70. // Structs can be marshalled/unmarshalled to/from BSON. When transforming structs to/from BSON, the following rules
  71. // apply:
  72. //
  73. // 1. Only exported fields in structs will be marshalled or unmarshalled.
  74. //
  75. // 2. When marshalling a struct, each field will be lowercased to generate the key for the corresponding BSON element.
  76. // For example, a struct field named "Foo" will generate key "foo". This can be overriden via a struct tag (e.g.
  77. // `bson:"fooField"` to generate key "fooField" instead).
  78. //
  79. // 3. An embedded struct field is marshalled as a subdocument. The key will be the lowercased name of the field's type.
  80. //
  81. // 4. A pointer field is marshalled as the underlying type if the pointer is non-nil. If the pointer is nil, it is
  82. // marshalled as a BSON null value.
  83. //
  84. // 5. When unmarshalling, a field of type interface{} will follow the D/M type mappings listed above. BSON documents
  85. // unmarshalled into an interface{} field will be unmarshalled as a D.
  86. //
  87. // The following struct tags can be used to configure behavior:
  88. //
  89. // 1. omitempty: If the omitempty struct tag is specified on a field, the field will not be marshalled if it is set to
  90. // the zero value. By default, a struct field is only considered empty if the field's type implements the Zeroer
  91. // interface and the IsZero method returns true. Struct fields of types that do not implement Zeroer are always
  92. // marshalled as embedded documents.
  93. //
  94. // 2. minsize: If the minsize struct tag is specified on a field of type int64, uint, uint32, or uint64 and the value of
  95. // the field can fit in a signed int32, the field will be serialized as a BSON int32 rather than a BSON int64. For other
  96. // types, this tag is ignored.
  97. //
  98. // 3. truncate: If the truncate struct tag is specified on a field with a non-float numeric type, BSON doubles unmarshalled
  99. // into that field will be trucated at the decimal point. For example, if 3.14 is unmarshalled into a field of type int,
  100. // it will be unmarshalled as 3. If this tag is not specified, the decoder will throw an error if the value cannot be
  101. // decoded without losing precision. For float64 or non-numeric types, this tag is ignored.
  102. //
  103. // 4. inline: If the inline struct tag is specified for a struct or map field, the field will be "flattened" when
  104. // marshalling and "un-flattened" when unmarshalling. This means that all of the fields in that struct/map will be
  105. // pulled up one level and will become top-level fields rather than being fields in a nested document. For example, if a
  106. // map field named "Map" with value map[string]interface{}{"foo": "bar"} is inlined, the resulting document will be
  107. // {"foo": "bar"} instead of {"map": {"foo": "bar"}}. There can only be one inlined map field in a struct. If there are
  108. // duplicated fields in the resulting document when an inlined field is marshalled, an error will be returned. This tag
  109. // can be used with fields that are pointers to structs. If an inlined pointer field is nil, it will not be marshalled.
  110. // For fields that are not maps or structs, this tag is ignored.
  111. //
  112. // Marshalling and Unmarshalling
  113. //
  114. // Manually marshalling and unmarshalling can be done with the Marshal and Unmarshal family of functions.
  115. package bson