bson

package module
v0.0.0-...-70852ae Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2011 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MaxKey = orderKey(1<<63 - 1)

Special value which compares higher than all other possible BSON values.

View Source
var MinKey = orderKey(-1 << 63)

Special value which compares lower than all other possible BSON values.

View Source
var Undefined undefined

Functions

func Marshal

func Marshal(in interface{}) (out []byte, err os.Error)

Marshal serializes the in document, which may be a map or a struct value. In the case of struct values, only exported fields will be serialized. These fields may optionally have tags to define the serialization key for the respective fields. Without a tag, the lowercased field name is used as the key for each field. If a field tag ends in "/c", that field will only be serialized if it's not set to the zero value for the field type. If a field tag ends with the "/s" suffix, an int64 value in the given field will be serialized as an int32 if possible.

func Unmarshal

func Unmarshal(in []byte, out interface{}) (err os.Error)

Unmarshal deserializes data from in into the out value. The out value must be a map or a pointer to a struct (or a pointer to a struct pointer). In the case of struct values, field names are mapped to the struct using the field tag as the key. If the field has no tag, its lowercased name will be used as the default key. Nil values are properly initialized when necessary.

The target field types of out may not necessarily match the BSON values of the provided data. If there is a sensible way to unmarshal the values into the Go types, they will be converted. Otherwise, the incompatible values will be silently skipped.

Types

type Binary

type Binary struct {
	Kind byte
	Data []byte
}

Representation for non-standard binary values. Any kind should work, but the following are known as of this writing:

0x00 - Generic. This is decoded as []byte(data), not Binary{0x00, data}.
0x01 - Function (!?)
0x02 - Obsolete generic.
0x03 - UUID
0x05 - MD5
0x80 - User defined.

type D

type D []DocElem

Type for dealing with documents containing ordered elements in a native fashion. For instance:

bson.D{{"a", 1}, {"b", true}}

In some situations, such as when creating indexes for MongoDB, the order in which the elements are defined is important. If the order is not important, using a map is generally more comfortable (see the bson.M type and the Map() method for D).

func (D) Map

func (d D) Map() (m M)

Build a map[string]interface{} out of the ordered element name/value pairs.

type DocElem

type DocElem struct {
	Name  string
	Value interface{}
}

See the bson.D type.

type Getter

type Getter interface {
	GetBSON() interface{}
}

Objects implementing the bson.Getter interface will get the GetBSON() method called when the given value has to be marshalled, and the result of this method will be marshaled in place of the actual object.

type JS

type JS struct {
	Code  string
	Scope interface{}
}

Special type for JavaScript code. If Scope is non-nil, it will be marshaled as a mapping from identifiers to values which should be used when evaluating the provided Code.

type M

type M map[string]interface{}

Handy alias for a map[string]interface{} map, useful for dealing with BSON in a native way. For instance:

bson.M{"a": 1, "b": true}

There's no special handling for this type in addition to what's done anyway for an equivalent map type. Elements in the map will be dumped in an undefined ordered. See also the bson.D type for an ordered alternative.

type MongoTimestamp

type MongoTimestamp int64

Special internal type used by MongoDB which for some strange reason has its own datatype defined in BSON.

type ObjectId

type ObjectId string

Unique ID identifying the BSON object. Must be exactly 12 bytes long. MongoDB objects by default have such a property set in their "_id" property.

http://www.mongodb.org/display/DOCS/Object+IDs

func NewObjectId

func NewObjectId() ObjectId

NewObjectId generates and returns a new unique ObjectId. This function causes a runtime error if it fails to get the hostname of the current machine.

func NewObjectIdSeconds

func NewObjectIdSeconds(sec int32) ObjectId

NewObjectIdSeconds returns a dummy ObjectId with the timestamp part filled with the provided number of seconds from epoch UTC, and all other parts filled with zeroes. It's not safe to insert a document with an id generated by this method, it is useful only for queries to find documents with ids generated before or after the specified timestamp.

func ObjectIdHex

func ObjectIdHex(s string) ObjectId

ObjectIdHex returns an ObjectId from the provided hex representation. Calling this function with an invalid hex representation will cause a runtime panic.

func (ObjectId) Counter

func (id ObjectId) Counter() int32

Counter returns the incrementing value part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) Machine

func (id ObjectId) Machine() []byte

Machine returns the 3-byte machine id part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) Pid

func (id ObjectId) Pid() uint16

Pid returns the process id part of the id. It's a runtime error to call this method with an invalid id.

func (ObjectId) String

func (id ObjectId) String() string

String returns a hex string representation of the id. Example: ObjectIdHex("4d88e15b60f486e428412dc9").

func (ObjectId) Timestamp

func (id ObjectId) Timestamp() int32

Timestamp returns the timestamp part of the id (the number of seconds from epoch in UTC). It's a runtime error to call this method with an invalid id.

func (ObjectId) ToString

func (id ObjectId) ToString() string

func (ObjectId) Valid

func (id ObjectId) Valid() bool

Valid returns true if the id is valid (contains exactly 12 bytes)

type Raw

type Raw struct {
	Kind byte
	Data []byte
}

Raw may be used to work with raw unprocessed BSON documents and elements, if necessary in advanced cases. Kind is the kind of element as defined per the BSON specification, and Data is the raw unprocessed data for the respective element.

Relevant documentation:

http://bsonspec.org/#/specification

func (Raw) Unmarshal

func (raw Raw) Unmarshal(out interface{}) (err os.Error)

Unmarshal deserializes raw into the out value. In addition to whole documents, Raw's Unmarshal may also be used to unmarshal the data for individual elements within a partially unmarshalled document. This enables parts of a document to be lazily and conditionally deserialized.

If the out value type is not compatible with raw, a *bson.TypeError is returned.

type RegEx

type RegEx struct {
	Pattern string
	Options string
}

A special type for regular expressions. The Options field should contain individual characters defining the way in which the pattern should be applied, and must be sorted. Valid options as of this writing are 'i' for case insensitive matching, 'm' for multi-line matching, 'x' for verbose mode, 'l' to make \w, \W, and similar be locale-dependent, 's' for dot-all mode (a '.' matches everything), and 'u' to make \w, \W, and similar match unicode. The value of the Options parameter is not verified before being marshaled into the BSON format.

type Setter

type Setter interface {
	SetBSON(v interface{}) (ok bool)
}

Objects implementing the bson.Setter interface will receive the BSON value via the SetBSON method during unmarshaling, and will not be changed as usual. If setting the value works, the method should return true. If it returns false, the given value will be omitted from maps and slices.

type Symbol

type Symbol string

Similar to a string, but used in languages with a distinct symbol type. This is an alias to a string type, so it can be used in string contexts and string(symbol) will work correctly.

type Timestamp

type Timestamp int64

UTC timestamp defined as nanoseconds since the traditional epoch time. The internal MongoDB representation stores this value as milliseconds, so some precision will be lost when sending a Go value to MongoDB, but given that Go most commonly uses nanoseconds in time-related operations, this conversion is convenient.

func Now

func Now() Timestamp

Now returns a Timestamp value with the current time in nanoseconds.

type TypeError

type TypeError struct {
	Type reflect.Type
	Kind byte
}

func (*TypeError) String

func (e *TypeError) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL