v0.7.0
Menu

#Wire format

The server speaks two formats on the same port, and picks one from the first byte of the connection. The requests and replies are the same documents in both. For the commands themselves see requests and replies.

#JSON lines

Each request and each reply is one line of JSON ending in a newline. The first line on a connection has to start with {. You can type requests into nc:

nc 127.0.0.1 7878
{"cmd":"use","db":"shop"}
{"cmd":"find","collection":"users","filter":{}}

JSON has no ObjectId or date type, so a value that needs one is a document with one special key:

  • {"$oid": "<24 hex digits>"} is an _id.
  • {"$date": <milliseconds>} is a date.
  • {"$regularExpression": {"pattern": "...", "options": "i"}} is a stored regex.

Anything else with those keys ({"$date": "x"}, or extra fields next to them) is an ordinary document. NaN and infinity have no JSON form and are refused. In JSON 1 and 1.0 are an int and a float, and floats are always written with a ..

Replies come in the order of the requests, with no request ids, so a client can pipeline requests on one connection.

#The binary wire

The same requests and replies as documents, without the text. It is not BSON. It is the encoding the log uses, so the server can decode a request without parsing text.

The client sends these four bytes first, once: FE 66 72 31. FE can never appear at the start of UTF-8 text, so the server tells the formats apart by the first byte. After that, every message in both directions is the length of the document as a u32, then the document. All numbers are little endian.

connection:  FE 66 72 31  |  len (u32) doc  |  len (u32) doc  | ...
document:    field count (u32), then for each field: name, value
name/string: length (u32), UTF-8 bytes
value:       tag byte, then the data for that tag
tag type after the tag
0 null nothing
1 false nothing
2 true nothing
3 int i64
4 float f64
5 string length (u32), bytes
6 ObjectId 12 bytes
7 array count (u32), then the values
8 document a document
9 date i64, milliseconds
10 regex pattern string, options string (i, m, s, x)

Notes:

  • A request is a document like {"cmd": "find", "collection": "users", "filter": {...}}.
  • An int and a float are different on the wire, so limit, skip and sort directions have to be sent as ints.
  • ObjectIds and dates have their own tags. A client document with exactly one key, {"$date": int} or {"$oid": "24 hex"}, is read as a date or an id, as it is over JSON.
  • Tag 10 holds a stored regex, as two strings. The viewer's binary client was not checked against it.
  • A length over the limit is a limit reply and the connection is closed. A body that ends early closes the connection. A length that lies does not make the server allocate that much.
  • NaN and infinity are refused, as over JSON.
  • Both formats share the limits: a message of at most 16 MiB (4 KiB before a login), 1,000,000 values, 100 levels. See memory and limits.
  • Documents in replies have their keys sorted by name in both formats.
  • Login with the token or SCRAM is the same two or three messages. nc cannot talk binary, but the same server takes JSON clients on the same port.

Anything that does not start with { or the four magic bytes is closed.

#Which to use

JSON is the default and is easy to read and debug. Binary skips building and parsing text. In the ferrite-db package, a 50,000 document insertMany took about 30% less time and a 250,000 document find about 25% less over binary on a local server. Small requests were about the same.

Both clients choose with format=json or format=binary in the connection string.