Documentation ¶
Overview ¶
Example (Types) ¶
package main import ( "fmt" "github.com/cristalhq/bson" ) func main() { arr := bson.A{"a", "b", "c", 12345} doc := bson.D{{"hello", "world"}, {"answer", 42}} doo := bson.M{"hello": "world", "pi": 3.14159} fmt.Println(arr) fmt.Println(arr.AsD()) fmt.Println() fmt.Println(doc) fmt.Println(doc.AsM()) fmt.Println() fmt.Println(doo) fmt.Println(doo.AsD()) _, _, _ = arr, doc, doo }
Output: [a b c 12345] [{0 a} {1 b} {2 c} {3 12345}] [{hello world} {answer 42}] map[answer:42 hello:world] map[hello:world pi:3.14159] [{hello world} {pi 3.14159}]
Index ¶
- Variables
- func Marshal(v any) ([]byte, error)
- func MarshalTo(dst []byte, v any) ([]byte, error)
- func Unmarshal(data []byte, v any) error
- type A
- type Appender
- type D
- type Decoder
- type Encoder
- type M
- type Marshaler
- type ObjectID
- func (oid *ObjectID) MarshalBSON() ([]byte, error)
- func (oid ObjectID) MarshalBinary() ([]byte, error)
- func (oid ObjectID) MarshalJSON() ([]byte, error)
- func (oid ObjectID) MarshalText() ([]byte, error)
- func (oid ObjectID) String() string
- func (oid *ObjectID) UnmarshalBSON(b []byte) error
- func (oid *ObjectID) UnmarshalBinary(b []byte) error
- func (oid *ObjectID) UnmarshalJSON(b []byte) error
- func (oid *ObjectID) UnmarshalText(b []byte) error
- type RawArray
- type RawObject
- type Regex
- type Timestamp
- func (ts Timestamp) Counter() uint32
- func (ts Timestamp) MarshalBSON() ([]byte, error)
- func (ts Timestamp) MarshalBinary() ([]byte, error)
- func (ts Timestamp) MarshalJSON() ([]byte, error)
- func (ts Timestamp) MarshalText() ([]byte, error)
- func (ts Timestamp) String() string
- func (ts Timestamp) Time() time.Time
- func (ts *Timestamp) UnmarshalBSON(b []byte) error
- func (ts *Timestamp) UnmarshalBinary(b []byte) error
- func (ts *Timestamp) UnmarshalJSON(b []byte) error
- func (ts *Timestamp) UnmarshalText(b []byte) error
- type Type
- type Unmarshaler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBadObjectID = errors.New("not a valid ObjectID")
Functions ¶
func Marshal ¶
Marshal returns BSON encoding of v.
Example ¶
package main import ( "encoding/hex" "fmt" "github.com/cristalhq/bson" ) func main() { arr := bson.A{"a", "b", "c"} b, err := bson.Marshal(arr) if err != nil { panic(err) } fmt.Println(hex.EncodeToString(b)) }
Output: 2000000002300002000000610002310002000000620002320002000000630000
func MarshalTo ¶
MarshalTo returns BSON encoding of v written to dst.
Example ¶
package main import ( "encoding/hex" "fmt" "github.com/cristalhq/bson" ) func main() { arr := bson.A{"a", "b", "c"} buf := make([]byte, 0, 128) buf, err := bson.MarshalTo(buf, arr) if err != nil { panic(err) } fmt.Println(hex.EncodeToString(buf)) }
Output: 2000000002300002000000610002310002000000620002320002000000630000
Types ¶
type A ¶
type A []any
A is a BSON array.
Example:
bson.A{"hello", "world", 3.14159, bson.D{{"foo", 12345}}}
type Appender ¶ added in v0.0.7
Appender is the interface implemented by types that can append marshaled BSON representation of itself.
type D ¶
type D []e
D is an ordered representation of a BSON document.
Example usage:
bson.D{{"hello", "world"}, {"foo", "bar"}, {"pi", 3.14159}}
type Decoder ¶ added in v0.0.5
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads and decodes BSON values from an input stream.
func NewDecodeBytes ¶ added in v0.0.5
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder writes BSON values to an output stream.
func NewEncoder ¶
NewEncoder returns a new encoder that writes to w.
type M ¶
M is an unordered representation of a BSON document.
Example usage:
bson.M{"hello": "world", "foo": "bar", "pi": 3.14159}
type Marshaler ¶
Marshaler is the interface implemented by types that can marshal themselves into valid BSON.
type ObjectID ¶
type ObjectID [12]byte
ObjectID represents BSON object ID.
func NewObjectIDWithTime ¶
NewObjectIDWithTime returns a new ObjectID.
func (*ObjectID) MarshalBSON ¶
MarshalBSON implements bson.Marshaler.
func (ObjectID) MarshalBinary ¶ added in v0.0.7
MarshalBinary implements encoding.BinaryMarshaler.
func (ObjectID) MarshalJSON ¶ added in v0.0.7
MarshalJSON implements json.Marshaler.
func (ObjectID) MarshalText ¶ added in v0.0.7
MarshalText implements encoding.TextMarshaler.
func (ObjectID) String ¶
String returns a hex string representation of the id. Example: ObjectID('64d526fa37931c1e97eea90f').
func (*ObjectID) UnmarshalBSON ¶
UnmarshalBSON implements bson.Unmarshaler.
func (*ObjectID) UnmarshalBinary ¶ added in v0.0.7
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*ObjectID) UnmarshalJSON ¶ added in v0.0.7
UnmarshalJSON implements json.Unmarshaler.
func (*ObjectID) UnmarshalText ¶ added in v0.0.7
UnmarshalText implements encoding.TextUnmarshaler.
type RawArray ¶ added in v0.0.7
type RawArray []byte
RawArray represents a raw array which will be encoded or decoded as is.
type RawObject ¶ added in v0.0.7
type RawObject []byte
RawObject represents a raw object which will be encoded or decoded as is.
type Regex ¶ added in v0.0.4
Regex represents BSON regular expression.
func (Regex) Compile ¶ added in v0.0.4
Compile returns regexp.Regexp.
func (*Regex) MarshalBSON ¶ added in v0.0.4
MarshalBSON implements bson.Marshaler.
func (*Regex) UnmarshalBSON ¶ added in v0.0.4
UnmarshalBSON implements bson.Unmarshaler.
type Timestamp ¶
type Timestamp int64
Timestamp represents BSON type Timestamp.
func NewTimestamp ¶
NewTimestamp returns a timestamp with a given time.
func NewTimestampWithCounter ¶
NewTimestampWithCounter returns a timestamp with a given time and counter.
func NowTimestamp ¶
func NowTimestamp() Timestamp
NowTimestamp returns a timestamp with current UTC time.
func (Timestamp) MarshalBSON ¶
MarshalBSON implements Marshaler.
func (Timestamp) MarshalBinary ¶ added in v0.0.6
MarshalBinary implements encoding.BinaryMarshaler.
func (Timestamp) MarshalJSON ¶ added in v0.0.6
MarshalJSON implements json.Marshaler.
func (Timestamp) MarshalText ¶ added in v0.0.6
MarshalText implements encoding.TextMarshaler.
func (Timestamp) String ¶
String returns a hex string representation of the id. Example: Timestamp('4398046513152, 1024').
func (*Timestamp) UnmarshalBSON ¶
UnmarshalBSON implements Unmarshaler.
func (*Timestamp) UnmarshalBinary ¶ added in v0.0.6
UnmarshalBinary implements encoding.BinaryUnmarshaler.
func (*Timestamp) UnmarshalJSON ¶ added in v0.0.6
UnmarshalJSON implements json.Unmarshaler.
func (*Timestamp) UnmarshalText ¶ added in v0.0.6
UnmarshalText implements encoding.TextUnmarshaler.
type Type ¶
type Type byte
Type represents a BSON type.
const ( TypeDouble Type = 0x01 TypeString Type = 0x02 TypeDocument Type = 0x03 TypeArray Type = 0x04 TypeBinary Type = 0x05 TypeUndefined Type = 0x06 TypeObjectID Type = 0x07 TypeBool Type = 0x08 TypeDateTime Type = 0x09 TypeNull Type = 0x0a TypeRegex Type = 0x0b TypeDBPointer Type = 0x0c TypeCodeWithScope Type = 0x0d TypeSymbol Type = 0x0e TypeJavaScriptScope Type = 0x0f TypeInt32 Type = 0x10 TypeTimestamp Type = 0x11 TypeInt64 Type = 0x12 TypeDecimal Type = 0x13 TypeMinKey Type = 0xff TypeMaxKey Type = 0x7f )
BSON element types as described in https://bsonspec.org/spec.html.
type Unmarshaler ¶
Unmarshaler is the interface implemented by types that can unmarshal a BSON representation of themselves.
The input can be assumed to be a valid encoding of a BSON. UnmarshalBSON must copy the BSON data if it wishes to retain the data after returning.