Module Bson

module Bson: sig .. end
This module includes a Bson document data structure, together with its encoding (to bytes) and decoding (from bytes).

The logic of usage is like this

The functions inside this module seem to be many, however, most of them are just for creating elements. These functions are to hide the implementation details of the type elements. Also, in this way, the Bson document can be used more safely.

Please refer to the Official Bson specification for more information.

Version 0.88.0


exception Invalid_objectId
Raised when an objectId's length is not 12. see http://bsonspec.org/#/specification
exception Wrong_bson_type
Raised when an unkown bson type is met while encoding the bson doc
exception Wrong_string
Raised only when trying to decode the bytes to string.
exception Malformed_bson
Raised when bad things happen while decoding the bytes to bson doc
type t 
The type for the Bson document. This is the main data structure
type special = 
| NULL
| MINKEY
| MAXKEY
The type for representing the special fields in Bson
type element 
The type for the fields for the Bson document

Basic operations on Bson document

val empty : t
The empty Bson document
val is_empty : t -> bool
Check whether this Bson document empty or not
val add_element : string -> element -> t -> t
Create an empty Bson document
val get_element : string -> t -> element
Get an element from a Bson document via its name
val remove_element : string -> t -> t
Remove an element from a Bson document
val encode : t -> string
Encode a Bson document to bytes (using type string as a carrier)
val decode : string -> t
Decode bytes (assuming type string as a carrier) to a Bson document

Creating elements

val create_double : float -> element
val create_string : string -> element
val create_doc_element : t -> element
val create_list : element list -> element
val create_user_binary : string -> element
val create_objectId : string -> element
val create_boolean : bool -> element
val create_utc : int64 -> element
val create_null : unit -> element
val create_regex : string -> string -> element
val create_jscode : string -> element
val create_jscode_w_s : string -> t -> element
val create_int32 : int32 -> element
val create_int64 : int64 -> element
val create_minkey : unit -> element
val create_maxkey : unit -> element

Getting raw values from elements

val get_double : element -> float
val get_string : element -> string
val get_doc_element : element -> t
val get_list : element -> element list
val get_generic_binary : element -> string
val get_function_binary : element -> string
val get_uuid_binary : element -> string
val get_md5_binary : element -> string
val get_user_binary : element -> string
val get_objectId : element -> string
val get_boolean : element -> bool
val get_utc : element -> int64
val get_null : element -> special
val get_regex : element -> string * string
val get_jscode : element -> string
val get_jscode_w_s : element -> string * t
val get_int32 : element -> int32
val get_int64 : element -> int64
val get_timestamp : element -> int64
val get_minkey : element -> special
val get_maxkey : element -> special

Experimental. Convert a Bson document to Json.

val to_simple_json : t -> string