Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var MaxKey = orderKey(1<<63 - 1)
Special value which compares higher than all other possible BSON values.
var MinKey = orderKey(-1 << 63)
Special value which compares lower than all other possible BSON values.
var Undefined undefined
Functions ¶
func Marshal ¶
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 ¶
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 ¶
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).
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
String returns a hex string representation of the id. Example: ObjectIdHex("4d88e15b60f486e428412dc9").
type Raw ¶
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 ¶
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 ¶
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.