package primitive
import "go.mongodb.org/mongo-driver/bson/primitive"
Package primitive contains types similar to Go primitives for BSON types that do not have direct Go primitive representations.
Index ¶
- Constants
- Variables
- func CompareTimestamp(tp, tp2 Timestamp) int
- func IsValidObjectID(s string) bool
- type A
- type Binary
- type CodeWithScope
- type D
- type DBPointer
- func (d DBPointer) Equal(d2 DBPointer) bool
- func (d DBPointer) IsZero() bool
- func (d DBPointer) String() string
- type DateTime
- func NewDateTimeFromTime(t time.Time) DateTime
- func (d DateTime) MarshalJSON() ([]byte, error)
- func (d DateTime) Time() time.Time
- func (d *DateTime) UnmarshalJSON(data []byte) error
- type Decimal128
- func NewDecimal128(h, l uint64) Decimal128
- func ParseDecimal128(s string) (Decimal128, error)
- func ParseDecimal128FromBigInt(bi *big.Int, exp int) (Decimal128, bool)
- func (d Decimal128) BigInt() (*big.Int, int, error)
- func (d Decimal128) GetBytes() (uint64, uint64)
- func (d Decimal128) IsInf() int
- func (d Decimal128) IsNaN() bool
- func (d Decimal128) IsZero() bool
- func (d Decimal128) MarshalJSON() ([]byte, error)
- func (d Decimal128) String() string
- func (d *Decimal128) UnmarshalJSON(b []byte) error
- type E
- type JavaScript
- type M
- type MaxKey
- type MinKey
- type Null
- type ObjectID
- func NewObjectID() ObjectID
- func NewObjectIDFromTimestamp(timestamp time.Time) ObjectID
- func ObjectIDFromHex(s string) (ObjectID, error)
- func (id ObjectID) Hex() string
- func (id ObjectID) IsZero() bool
- func (id ObjectID) MarshalJSON() ([]byte, error)
- func (id ObjectID) MarshalText() ([]byte, error)
- func (id ObjectID) String() string
- func (id ObjectID) Timestamp() time.Time
- func (id *ObjectID) UnmarshalJSON(b []byte) error
- func (id *ObjectID) UnmarshalText(b []byte) error
- type Regex
- type Symbol
- type Timestamp
- func (tp Timestamp) After(tp2 Timestamp) bool
- func (tp Timestamp) Before(tp2 Timestamp) bool
- func (tp Timestamp) Compare(tp2 Timestamp) int
- func (tp Timestamp) Equal(tp2 Timestamp) bool
- func (tp Timestamp) IsZero() bool
- type Undefined
Constants ¶
const ( MaxDecimal128Exp = 6111 MinDecimal128Exp = -6176 )
These constants are the maximum and minimum values for the exponent field in a decimal128 value.
Variables ¶
var ( ErrParseNaN = errors.New("cannot parse NaN as a *big.Int") ErrParseInf = errors.New("cannot parse Infinity as a *big.Int") ErrParseNegInf = errors.New("cannot parse -Infinity as a *big.Int") )
These errors are returned when an invalid value is parsed as a big.Int.
ErrInvalidHex indicates that a hex string cannot be converted to an ObjectID.
Functions ¶
func CompareTimestamp ¶
CompareTimestamp compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after tp2, it returns +1; if they're the same, it returns 0.
Deprecated: Use Timestamp.Compare instead.
func IsValidObjectID ¶
IsValidObjectID returns true if the provided hex string represents a valid ObjectID and false if not.
Deprecated: Use ObjectIDFromHex and check the error instead.
Types ¶
type A ¶
type A []interface{}
An A is an ordered representation of a BSON array.
Example usage:
bson.A{"bar", "world", 3.14159, bson.D{{"qux", 12345}}}
type Binary ¶
Binary represents a BSON binary value.
func (Binary) Equal ¶
Equal compares bp to bp2 and returns true if they are equal.
func (Binary) IsZero ¶
IsZero returns if bp is the empty Binary.
type CodeWithScope ¶
type CodeWithScope struct { Code JavaScript Scope interface{} }
CodeWithScope represents a BSON JavaScript code with scope value.
func (CodeWithScope) String ¶
func (cws CodeWithScope) String() string
type D ¶
type D []E
D is an ordered representation of a BSON document. This type should be used when the order of the elements matters, such as MongoDB command documents. If the order of the elements does not matter, an M should be used instead.
Example usage:
bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
func (D) Map ¶
Map creates a map from the elements of the D.
Deprecated: Converting directly from a D to an M will not be supported in Go Driver 2.0. Instead, users should marshal the D to BSON using bson.Marshal and unmarshal it to M using bson.Unmarshal.
type DBPointer ¶
DBPointer represents a BSON dbpointer value.
func (DBPointer) Equal ¶
Equal compares d to d2 and returns true if they are equal.
func (DBPointer) IsZero ¶
IsZero returns if d is the empty DBPointer.
func (DBPointer) String ¶
type DateTime ¶
type DateTime int64
DateTime represents the BSON datetime value.
func NewDateTimeFromTime ¶
NewDateTimeFromTime creates a new DateTime from a Time.
func (DateTime) MarshalJSON ¶
MarshalJSON marshal to time type.
func (DateTime) Time ¶
Time returns the date as a time type.
func (*DateTime) UnmarshalJSON ¶
UnmarshalJSON creates a primitive.DateTime from a JSON string.
type Decimal128 ¶
type Decimal128 struct {
// contains filtered or unexported fields
}
Decimal128 holds decimal128 BSON values.
func NewDecimal128 ¶
func NewDecimal128(h, l uint64) Decimal128
NewDecimal128 creates a Decimal128 using the provide high and low uint64s.
func ParseDecimal128 ¶
func ParseDecimal128(s string) (Decimal128, error)
ParseDecimal128 takes the given string and attempts to parse it into a valid Decimal128 value.
func ParseDecimal128FromBigInt ¶
func ParseDecimal128FromBigInt(bi *big.Int, exp int) (Decimal128, bool)
ParseDecimal128FromBigInt attempts to parse the given significand and exponent into a valid Decimal128 value.
func (Decimal128) BigInt ¶
func (d Decimal128) BigInt() (*big.Int, int, error)
BigInt returns significand as big.Int and exponent, bi * 10 ^ exp.
func (Decimal128) GetBytes ¶
func (d Decimal128) GetBytes() (uint64, uint64)
GetBytes returns the underlying bytes of the BSON decimal value as two uint64 values. The first contains the most first 8 bytes of the value and the second contains the latter.
func (Decimal128) IsInf ¶
func (d Decimal128) IsInf() int
IsInf returns:
+1 d == Infinity 0 other case -1 d == -Infinity
func (Decimal128) IsNaN ¶
func (d Decimal128) IsNaN() bool
IsNaN returns whether d is NaN.
func (Decimal128) IsZero ¶
func (d Decimal128) IsZero() bool
IsZero returns true if d is the empty Decimal128.
func (Decimal128) MarshalJSON ¶
func (d Decimal128) MarshalJSON() ([]byte, error)
MarshalJSON returns Decimal128 as a string.
func (Decimal128) String ¶
func (d Decimal128) String() string
String returns a string representation of the decimal value.
func (*Decimal128) UnmarshalJSON ¶
func (d *Decimal128) UnmarshalJSON(b []byte) error
UnmarshalJSON creates a primitive.Decimal128 from a JSON string, an extended JSON $numberDecimal value, or the string "null". If b is a JSON string or extended JSON value, d will have the value of that string, and if b is "null", d will be unchanged.
type E ¶
type E struct { Key string Value interface{} }
E represents a BSON element for a D. It is usually used inside a D.
type JavaScript ¶
type JavaScript string
JavaScript represents a BSON JavaScript code value.
type M ¶
type M map[string]interface{}
M is an unordered representation of a BSON document. This type should be used when the order of the elements does not matter. This type is handled as a regular map[string]interface{} when encoding and decoding. Elements will be serialized in an undefined, random order. If the order of the elements matters, a D should be used instead.
Example usage:
bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
type MaxKey ¶
type MaxKey struct{}
MaxKey represents the BSON maxkey value.
type MinKey ¶
type MinKey struct{}
MinKey represents the BSON minkey value.
type Null ¶
type Null struct{}
Null represents the BSON null value.
type ObjectID ¶
type ObjectID [12]byte
ObjectID is the BSON ObjectID type.
var NilObjectID ObjectID
NilObjectID is the zero value for ObjectID.
func NewObjectID ¶
func NewObjectID() ObjectID
NewObjectID generates a new ObjectID.
func NewObjectIDFromTimestamp ¶
NewObjectIDFromTimestamp generates a new ObjectID based on the given time.
func ObjectIDFromHex ¶
ObjectIDFromHex creates a new ObjectID from a hex string. It returns an error if the hex string is not a valid ObjectID.
func (ObjectID) Hex ¶
Hex returns the hex encoding of the ObjectID as a string.
func (ObjectID) IsZero ¶
IsZero returns true if id is the empty ObjectID.
func (ObjectID) MarshalJSON ¶
MarshalJSON returns the ObjectID as a string
func (ObjectID) MarshalText ¶
MarshalText returns the ObjectID as UTF-8-encoded text. Implementing this allows us to use ObjectID as a map key when marshalling JSON. See https://pkg.go.dev/encoding#TextMarshaler
func (ObjectID) String ¶
func (ObjectID) Timestamp ¶
Timestamp extracts the time part of the ObjectId.
func (*ObjectID) UnmarshalJSON ¶
UnmarshalJSON populates the byte slice with the ObjectID. If the byte slice is 24 bytes long, it will be populated with the hex representation of the ObjectID. If the byte slice is twelve bytes long, it will be populated with the BSON representation of the ObjectID. This method also accepts empty strings and decodes them as NilObjectID. For any other inputs, an error will be returned.
func (*ObjectID) UnmarshalText ¶
UnmarshalText populates the byte slice with the ObjectID. Implementing this allows us to use ObjectID as a map key when unmarshalling JSON. See https://pkg.go.dev/encoding#TextUnmarshaler
type Regex ¶
Regex represents a BSON regex value.
func (Regex) Equal ¶
Equal compares rp to rp2 and returns true if they are equal.
func (Regex) IsZero ¶
IsZero returns if rp is the empty Regex.
func (Regex) String ¶
type Symbol ¶
type Symbol string
Symbol represents a BSON symbol value.
type Timestamp ¶
Timestamp represents a BSON timestamp value.
func (Timestamp) After ¶
After reports whether the time instant tp is after tp2.
func (Timestamp) Before ¶
Before reports whether the time instant tp is before tp2.
func (Timestamp) Compare ¶
Compare compares the time instant tp with tp2. If tp is before tp2, it returns -1; if tp is after tp2, it returns +1; if they're the same, it returns 0.
func (Timestamp) Equal ¶
Equal compares tp to tp2 and returns true if they are equal.
func (Timestamp) IsZero ¶
IsZero returns if tp is the zero Timestamp.
type Undefined ¶
type Undefined struct{}
Undefined represents the BSON undefined value type.
Source Files ¶
decimal.go objectid.go primitive.go
- Version
- v1.17.3 (latest)
- Published
- Feb 25, 2025
- Platform
- linux/amd64
- Imports
- 15 packages
- Last checked
- 12 hours ago –
Tools for package owners.