Module bson

A BSON document is a JSON-like object with a standard binary encoding defined at bsonspec.org.

Description

A BSON document is a JSON-like object with a standard binary encoding defined at bsonspec.org. This implements version 1.0 of that spec.

Data Types

arr()

arr() = [value()]

Caution, a string() will be interpreted as an array of integers. You must supply strings as utf8 binary, see below.

bfunction()

bfunction() = {bin, function, binary()}

bin()

bin() = {bin, bin, binary()}

bin_old()

bin_old() = {bin, bin_old, binary()}

document()

document() = tuple()

{label(), value(), label(), value(), ...}. Conceptually a document is a list of label-value pairs (associative array, dictionary, record). However, for read/write-ability, it is implemented as a flat tuple, ie. the list becomes a tuple and the pair braces are elided, so you just have alternating labels and values where each value is associated with the previous label. To distinguish a tagged value such as {uuid, _} (see value() type below) from a document with field name 'uuid' we made sure all valid tagged value types have an odd number of elements (documents have even number of elements). So actually only {bin, uuid, _} is a valid value, {uuid, _} is a document.

javascript()

javascript() = {javascript, document(), utf8()}

scope and code

label()

label() = binary() | atom()

md5()

md5() = {bin, md5, binary()}

minmaxkey()

minmaxkey() = 'MIN_KEY' | 'MAX_KEY'

Special values that compare lower/higher than all other bson values

mongostamp()

mongostamp() = {mongostamp, integer(), integer()}

4-byte increment, 4-byte timestamp. 0 timestamp has special semantics

objectid()

objectid() = {<<_:96>>}

<<UnixTimeSecs:32/big, MachineId:24/big, ProcessId:16/big, Count:24/big>>

regex()

regex() = {regex, utf8(), utf8()}

pattern and options

unixsecs()

unixsecs() = integer()

Unix Time in seconds

unixtime()

unixtime() = {integer(), integer(), integer()}

{MegaSecs, Secs, MicroSecs} Unix time in Erlang now/os:timstamp format, but only to millisecond precision when serialized.

userdefined()

userdefined() = {bin, userdefined, binary()}

utf8()

utf8() = unicode:unicode_binary()

binary() representing a string of characters encoded with UTF-8. An Erlang string() is a list of unicode characters (codepoints), but this list must be converted to utf-8 binary for use in Bson. Call utf8/1 to do this, or encode pure ascii literals directly as <<"abc">> and non-pure ascii literals as <<"a�c"/utf8>>.

uuid()

uuid() = {bin, uuid, binary()}

uuid_old()

uuid_old() = {bin, uuid_old, binary()}

value()

value() = float() | utf8() | document() | arr() | bin() | bfunction() | bin_old() | uuid_old() | uuid() | md5() | userdefined() | objectid() | boolean() | unixtime() | null | regex() | javascript() | atom() | integer() | mongostamp() | minmaxkey()

Function Index

append/2Append two documents together.
at/2Value of field in document, error if missing.
doc_foldl/3Reduce document by applying given function to each field with result of previous field's application, starting with given initial result.
doc_foldr/3Same as doc_foldl/3 except apply fields in reverse order.
document/1Convert list of fields to a document.
exclude/2Remove given fields from document.
fields/1Convert document to a list of all its fields.
flatten_map/1
include/2Project given fields of document.
lookup/2Value of field in document if there.
lookup/3Value of field in document if there or default.
merge/2First doc overrides second with new fields added at end of second doc.
merge/3
ms_precision/1Truncate microsecs to millisecs since bson drops microsecs anyway, so time will be equal before and after serialization.
objectid/3
objectid_time/1Time when object id was generated.
secs_to_unixtime/1
str/1Convert utf8 binary to string.
timenow/0
unixtime_to_secs/1
update/3Replace field with new value, adding to end if new.
utf8/1Convert string to utf8 binary.

Function Details

append/2

append(Doc1::document(), Doc2::document()) -> document()

Append two documents together

at/2

at(Label::label(), Document::document()) -> value()

Value of field in document, error if missing

doc_foldl/3

doc_foldl(Fun::fun((label(), value(), A) -> A), A, Doc::document() | map()) -> A

Reduce document by applying given function to each field with result of previous field's application, starting with given initial result.

doc_foldr/3

doc_foldr(Fun::fun((label(), value(), A) -> A), A, Doc::document()) -> A

Same as doc_foldl/3 except apply fields in reverse order

document/1

document(Fields::[{label(), value()}]) -> document()

Convert list of fields to a document

exclude/2

exclude(Labels::[label()], Document::document()) -> document()

Remove given fields from document

fields/1

fields(Doc::document()) -> [{label(), value()}]

Convert document to a list of all its fields

flatten_map/1

flatten_map(Map::map()) -> map()

include/2

include(Labels::[label()], Document::document()) -> document()

Project given fields of document

lookup/2

lookup(Label::label(), Doc::document()) -> value() | {}

Value of field in document if there

lookup/3

lookup(Label::label(), Doc::document(), Default::value()) -> value()

Value of field in document if there or default

merge/2

merge(UpDoc::document(), BaseDoc::document()) -> document()

First doc overrides second with new fields added at end of second doc

merge/3

merge(UpDoc::document(), BaseDoc::document(), Fun::fun((label(), value(), value()) -> value())) -> document()

ms_precision/1

ms_precision(X1::unixtime()) -> unixtime()

Truncate microsecs to millisecs since bson drops microsecs anyway, so time will be equal before and after serialization.

objectid/3

objectid(UnixSecs::unixsecs(), MachineAndProcId::<<_:40>>, Count::integer()) -> objectid()

objectid_time/1

objectid_time(X1::objectid()) -> unixtime()

Time when object id was generated

secs_to_unixtime/1

secs_to_unixtime(UnixSecs::unixsecs()) -> unixtime()

str/1

str(CharData::unicode:chardata()) -> string()

Convert utf8 binary to string. utf8() is a subtype of unicode:chardata().

timenow/0

timenow() -> unixtime()

unixtime_to_secs/1

unixtime_to_secs(X1::unixtime()) -> unixsecs()

update/3

update(Label::label(), Value::value(), Document::document()) -> document()

Replace field with new value, adding to end if new

utf8/1

utf8(CharData::unicode:chardata()) -> utf8()

Convert string to utf8 binary. string() is a subtype of unicode:chardata().


Generated by EDoc