Databases Reference
In-Depth Information
BSON SPECIFICATION
MongoDB encodes the documents it stores in a JSON-like binary format called
BSON, which was introduced in Chapter 2 of this topic. I briefl y review a
few BSON characteristics here.
A BSON document is a collection of zero or more binary key/value pairs. The basic
binary types that make up BSON representations are as follows:
byte — 1 byte
int32 — 4 bytes
int64 — 8 bytes
double — 8 bytes
int32 and int64 correspond to the 32-bit and 64-bit signed integers, respectively.
Double corresponds to a 64-bit IEEE 754 fl oating-point value. A possible example
document could be as follows:
{ “hello”: “world” }
Such a document could be represented using BSON as follows:
“\x16\x00\x00\x00\x02hello\x00 \x06\x00\x00\x00world\x00\x00”
The BSON notations shown here use the familiar C semantics for binary value
representation, using Hexadecimal equivalents. If you were to map the readable
document to the binary notation, it would be as follows:
{ and }—”\x16\x00\x00\x00 and \x00
“hello”:—x02hello\x00
“world”—\x06\x00\x00\x00world\x00
You can read more about the BSON specifi cation at http://bsonspec.org/ .
MongoDB Wire Protocol
Clients speak to a MongoDB server using a simple TCP/IP-based socket connection. The wire
protocol used for the communication is a simple request-response-based socket protocol. The
wire protocol headers and payload are BSON encoded. The ordering of messages follows the little
endian format, which is the same as in BSON.
In a standard request-response model, a client sends a request to a server and the server responds
to the request. In terms of the wire protocol, a request is sent with a message header and a request
payload. A response comes back with a message header and a response payload. The format for the
message header between the request and the response is quite similar. However, the format of the
request and the response payload are not the same. Figure 13-1 depicts the basic request-response
communication between a client and a MongoDB server.
Search WWH ::




Custom Search