-
Notifications
You must be signed in to change notification settings - Fork 79
/
spec.html
403 lines (389 loc) · 15 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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>BSON (Binary JSON): Specification</title>
<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">signed_byte(n)</td>
<td>8-bit, two's complement signed integer for which the value is <code>n</code></td>
</tr>
<tr>
<td class="t">unsigned_byte(n)</td>
<td>8-bit unsigned integer for which the value is <code>n</code></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 we use the <code>*</code> operator as
shorthand for repetition (e.g. <code>(byte*2)</code>
is <code>byte byte</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 unsigned_byte(0)</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 style="width:35%" class="fix ex">signed_byte(1) 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">signed_byte(2) 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">signed_byte(3) e_name document</td>
<td>Embedded document</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(4) e_name document</td>
<td>Array</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(5) e_name binary</td>
<td>Binary data</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(6) 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">signed_byte(7) e_name (byte*12)</td>
<td><a href="https://www.mongodb.com/docs/manual/reference/bson-types/#objectid">ObjectId</a></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(8) e_name unsigned_byte(0)</td>
<td>Boolean - false</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(8) e_name unsigned_byte(1)</td>
<td>Boolean - true</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(9) e_name int64</td>
<td>UTC datetime</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(10) e_name</td>
<td>Null value</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(11) 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 option characters are <code>i</code> for
case insensitive matching, <code>m</code> for multiline
matching, <code>s</code> for <i>dotall</i> mode ("." matches
everything), <code>x</code> for verbose mode, and
<code>u</code> to make "\w", "\W", etc. match Unicode.
</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(12) 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">signed_byte(13) e_name string</td>
<td>JavaScript code</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(14) 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">signed_byte(15) e_name code_w_s</td>
<td>JavaScript code with scope — <em>Deprecated</em></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(16) 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">signed_byte(17) e_name uint64</td>
<td>Timestamp</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(18) 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">signed_byte(19) 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">signed_byte(-1) e_name</td>
<td>Min key</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">signed_byte(127) 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*) unsigned_byte(0)</td>
<td>String - The int32 is the number of bytes in the
(byte*) plus one for the trailing null byte. 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*) unsigned_byte(0)</td>
<td>Zero or more modified UTF-8 encoded characters
followed by the null byte. The (byte*) MUST NOT contain
<code>unsigned_byte(0)</code>, 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">unsigned_byte(0)</td>
<td>Generic binary subtype</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(1)</td>
<td>Function</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(2)</td>
<td>Binary (Old)</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(3)</td>
<td>UUID (Old)</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(4)</td>
<td>UUID</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(5)</td>
<td>MD5</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(6)</td>
<td><a
href="https://github.com/mongodb/specifications/blob/master/source/bson-binary-encrypted/binary-encrypted.md">Encrypted
BSON value</a></td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(7)</td>
<td>Compressed BSON column</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(8)</td>
<td>Sensitive</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(9)</td>
<td>Vector</td>
</tr>
<tr>
<td class="fix nt"></td>
<td class="fix op">|</td>
<td class="fix ex">unsigned_byte(128)—unsigned_byte(255)</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 with scope — <em>Deprecated</em></td>
</tr>
</table>
</div>
</div>
<h3>Notes</h3>
<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>Compressed BSON Column - Compact storage of BSON data. This data
type uses delta and delta-of-delta compression and
run-length-encoding for efficient element storage. Also has an
encoding for sparse arrays containing missing values.</li>
<li>Vector - A densely packed array of numbers, all of the same
type. This subtype supports the packed
binary (1-bit unsigned <code>int</code>), signed 8-bit
<code>int</code>, and 32-bit <code>float</code> element types.
</li>
<li>The BSON "binary" or <code>BinData</code> 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 0 to
127 are predefined or reserved. Subtypes from 128 to 255 are
user-defined.</li>
<ul>
<li><code>unsigned_byte(2)</code> Binary (Old) - This used to be the default
subtype, but was deprecated in favor of subtype 0.
Drivers and tools should be sure to handle subtype 2
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><code>unsigned_byte(3)</code> UUID (Old) - This used to be the UUID subtype,
but was deprecated in favor of subtype 4. Drivers
and tools for languages with a native UUID type should handle
subtype 3 appropriately.</li>
<li><code>unsigned_byte(128)—unsigned_byte(255)</code> User defined subtypes. The binary data can be
anything.</li>
</ul>
<li>Code with scope - <em>Deprecated.</em> 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>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-7301842-4");
pageTracker._trackPageview();
} catch (err) { }
</script>
</body>
</html>