forked from mongodb/bsonspec.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec.html
315 lines (304 loc) · 14.6 KB
/
spec.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<html>
<head>
<link rel="stylesheet" href="css/spec.css" type="text/css">
</head>
<body>
<h2 class="nojs">Specification Version 1.1</h2>
<p>BSON is a binary format in which zero or more ordered key/value
pairs are stored as a single entity. We call this entity
a <em>document</em>.</p>
<p>The following grammar specifies version 1.1 of the
BSON standard. We've written the grammar using a
pseudo-<a href="http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form">BNF</a>
syntax. Valid BSON data is represented by
the <code>document</code> non-terminal.</p>
<div id="grammar">
<h3>Basic Types</h3>
<p>The following basic types are used as terminals in
the rest of the grammar. Each type must be serialized in
little-endian format.</p>
<div class="tb">
<table>
<tr>
<td class="t">byte</td>
<td>1 byte (8-bits)</td>
</tr>
<tr>
<td class="t">int32</td>
<td>4 bytes (32-bit signed integer, two's complement)</td>
</tr>
<tr>
<td class="t">int64</td>
<td>8 bytes (64-bit signed integer, two's complement)</td>
</tr>
<tr>
<td class="t">uint64</td>
<td>8 bytes (64-bit unsigned integer)</td>
</tr>
<tr>
<td class="t">double</td>
<td>8 bytes (64-bit IEEE 754-2008 binary floating point)</td>
</tr>
<tr>
<td class="t">decimal128</td>
<td>16 bytes (128-bit IEEE 754-2008 decimal floating point)</td>
</tr>
</table>
</div>
<h3>Non-terminals</h3>
<p>The following specifies the rest of the BSON
grammar. Note that quoted strings represent terminals,
and should be interpreted with C semantics
(e.g. <code>"\x01"</code> represents the byte <code>0000
0001</code>). Also note that we use the <code>*</code>
operator as shorthand for repetition
(e.g. <code>("\x01"*2)</code>
is <code>"\x01\x01"</code>). When used as a unary
operator, <code>*</code> means that the repetition can
occur 0 or more times.</p>
<div class="tb">
<table>
<tr>
<td class="fix nt">document</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 e_list "\x00"</td>
<td >BSON Document. int32 is the total number of bytes comprising the document.</td>
</tr>
<tr class="section">
<td class="fix nt">e_list</td>
<td class="fix op">::=</td>
<td class="fix ex">element e_list</td>
<td></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">""</td>
<td></td>
</tr>
<tr class="section">
<td class="fix nt">element</td>
<td class="fix op">::=</td>
<td class="fix ex">"\x01" e_name double</td>
<td>64-bit binary floating point</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x02" e_name string</td>
<td>UTF-8 string</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x03" e_name document</td>
<td>Embedded document</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x04" e_name document</td>
<td >Array</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x05" e_name binary</td>
<td>Binary data</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x06" e_name</td>
<td>Undefined (value) — <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x07" e_name (byte*12)</td>
<td><a href="http://dochub.mongodb.org/core/objectids">ObjectId</a></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x08" e_name "\x00"</td>
<td>Boolean "false"</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x08" e_name "\x01"</td>
<td>Boolean "true"</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x09" e_name int64</td>
<td >UTC datetime</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0A" e_name</td>
<td>Null value</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0B" e_name cstring cstring</td>
<td >Regular expression - The first cstring is the regex pattern, the second is the regex options string. Options are identified by characters, which must be stored in alphabetical order. Valid options are 'i' for case insensitive matching, 'm' for multiline matching, 'x' for verbose mode, 'l' to make \w, \W, etc. locale dependent, 's' for dotall mode ('.' matches everything), and 'u' to make \w, \W, etc. match unicode.</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0C" e_name string (byte*12)</td>
<td>DBPointer — <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0D" e_name string</td>
<td>JavaScript code</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0E" e_name string</td>
<td> Symbol. <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x0F" e_name code_w_s</td>
<td>JavaScript code w/ scope</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x10" e_name int32</td>
<td>32-bit integer</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x11" e_name uint64</td>
<td >Timestamp</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x12" e_name int64</td>
<td>64-bit integer</td>
</tr>
<tr class="section">
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x13" e_name decimal128</td>
<td><a href="https://github.com/mongodb/specifications/blob/master/source/bson-decimal128/decimal128.rst">128-bit decimal floating point</a></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\xFF" e_name</td>
<td >Min key</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x7F" e_name</td>
<td >Max key</td>
</tr>
<tr class="section">
<td class="fix nt">e_name</td>
<td class="fix op">::=</td>
<td class="fix ex">cstring</td>
<td>Key name</td>
</tr>
<tr class="section">
<td class="fix nt">string</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 (byte*) "\x00"</td>
<td >String - The int32 is the number bytes in the (byte*) + 1 (for the trailing '\x00'). The (byte*) is zero or more UTF-8 encoded characters.</td>
</tr>
<tr class="section">
<td class="fix nt">cstring</td>
<td class="fix op">::=</td>
<td class="fix ex">(byte*) "\x00"</td>
<td >Zero or more modified UTF-8 encoded characters followed by '\x00'. The (byte*) MUST NOT contain '\x00', hence it is not full UTF-8.</td>
</tr>
<tr class="section">
<td class="fix nt">binary</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 subtype (byte*)</td>
<td >Binary - The int32 is the number of bytes in the (byte*).</td>
</tr>
<tr class="section">
<td class="fix nt">subtype</td>
<td class="fix op">::=</td>
<td class="fix ex">"\x00"</td>
<td >Generic binary subtype</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x01"</td>
<td>Function</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x02"</td>
<td >Binary (Old)</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x03"</td>
<td >UUID (Old)</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x04"</td>
<td>UUID</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x05"</td>
<td>MD5</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">"\x80"</td>
<td >User defined</td>
</tr>
<tr class="section">
<td class="fix nt">code_w_s</td>
<td class="fix op">::=</td>
<td class="fix ex">int32 string document</td>
<td >Code w/ scope</td>
</tr>
</table>
</div>
<h3>Notes</h3>
<p>
<ul>
<li >Array - The document for an array is a normal BSON document with integer values for the keys, starting with 0 and continuing sequentially. For example, the array ['red', 'blue'] would be encoded as the document {'0': 'red', '1': 'blue'}. The keys must be in ascending numerical order.</li>
<li >UTC datetime - The int64 is UTC milliseconds since the Unix epoch.</li>
<li>Timestamp - Special internal type used by MongoDB replication and sharding. First 4 bytes are an increment, second 4 are a timestamp.</li>
<li >Min key - Special type which compares lower than all other possible BSON element values.</li>
<li >Max key - Special type which compares higher than all other possible BSON element values.</li>
<li >Generic binary subtype - This is the most commonly used binary subtype and should be the 'default' for drivers and tools.</li>
<li>The BSON "binary" or "BinData" datatype is used to represent arrays of bytes. It is somewhat analogous to the Java notion of a ByteArray. BSON binary values have a <em>subtype</em>. This is used to indicate what kind of data is in the byte array. Subtypes from zero to 127 are predefined or reserved. Subtypes from 128-255 are user-defined.
<ul>
<li>\x02 Binary (Old) - This used to be the default subtype, but was deprecated in favor of \x00. Drivers and tools should be sure to handle \x02 appropriately. The structure of the binary data (the byte* array in the binary non-terminal) must be an int32 followed by a (byte*). The int32 is the number of bytes in the repetition.</li>
<li>\x03 UUID (Old) - This used to be the UUID subtype, but was deprecated in favor of \x04. Drivers and tools for languages with a native UUID type should handle \x03 appropriately.</li>
<li>\x80-\xFF "User defined" subtypes. The binary data can be anything.</li>
</ul>
</li>
<li >Code w/ scope - The int32 is the length in bytes of the entire code_w_s value. The string is JavaScript code. The document is a mapping from identifiers to values, representing the scope in which the string should be evaluated.</li>
</ul>
</p>
</body></html>