2
2
3
3
.. _ruby-bson-tutorial-4-0:
4
4
5
- =================
5
+ *****************
6
6
BSON 4.x Tutorial
7
- =================
7
+ *****************
8
8
9
9
.. default-domain:: mongodb
10
10
@@ -17,7 +17,7 @@ BSON 4.x Tutorial
17
17
This tutorial discusses using the Ruby BSON library.
18
18
19
19
Installation
20
- ------------
20
+ ============
21
21
22
22
The BSON library can be installed from `Rubygems <http://rubygems.org>`_
23
23
manually or with bundler.
@@ -37,7 +37,7 @@ To install the gem with bundler, include the following in your ``Gemfile``:
37
37
The BSON library is compatible with MRI >= 2.3 and JRuby >= 9.2.
38
38
39
39
Use With ActiveSupport
40
- ----------------------
40
+ ======================
41
41
42
42
Serialization for ActiveSupport-defined classes, such as TimeWithZone, is
43
43
not loaded by default to avoid a hard dependency of BSON on ActiveSupport.
@@ -50,7 +50,7 @@ ActiveSupport-related code must be explicitly required:
50
50
require 'bson/active_support'
51
51
52
52
BSON Serialization
53
- ------------------
53
+ ==================
54
54
55
55
Getting a Ruby object's raw BSON representation is done by calling ``to_bson``
56
56
on the Ruby object, which will return a ``BSON::ByteBuffer``. For example:
@@ -70,13 +70,13 @@ you wish to instantiate and passing it a ``BSON::ByteBuffer`` instance.
70
70
71
71
72
72
Byte Buffers
73
- ------------
73
+ ============
74
74
75
75
BSON library 4.0 introduces the use of native byte buffers in MRI and JRuby
76
76
instead of using ``StringIO``, for improved performance.
77
77
78
78
Writing
79
- ```````
79
+ -------
80
80
81
81
To create a ``ByteBuffer`` for writing (i.e. serializing to BSON),
82
82
instantiate ``BSON::ByteBuffer`` with no arguments:
@@ -184,7 +184,7 @@ over a socket), call ``to_s`` on the buffer:
184
184
185
185
186
186
Reading
187
- ```````
187
+ -------
188
188
189
189
To create a ``ByteBuffer`` for reading (i.e. deserializing from BSON),
190
190
instantiate ``BSON::ByteBuffer`` with a byte string as the argument:
@@ -218,7 +218,7 @@ To restart reading from the beginning of a buffer, use ``rewind``:
218
218
219
219
220
220
Supported Classes
221
- -----------------
221
+ =================
222
222
223
223
Core Ruby classes that have representations in the BSON specification and
224
224
will have a ``to_bson`` method defined for them are: ``Object``, ``Array``,
@@ -230,7 +230,7 @@ specific to the specification:
230
230
231
231
232
232
``BSON::Binary``
233
- ````````````````
233
+ ----------------
234
234
235
235
Use ``BSON::Binary`` objects to store arbitrary binary data. The ``Binary``
236
236
objects can be constructed from binary strings as follows:
@@ -281,7 +281,7 @@ The data and the subtype can be retrieved from ``Binary`` instances using
281
281
# => #<Encoding:ASCII-8BIT>
282
282
283
283
UUID Methods
284
- ~~~~~~~~~~~~
284
+ ````````````
285
285
286
286
To create a UUID BSON::Binary (binary subtype 4) from its RFC 4122-compliant
287
287
string representation, use the ``from_uuid`` method:
@@ -314,7 +314,7 @@ Note that the ``:standard`` representation can only be used with a Binary
314
314
of subtype ``:uuid`` (not ``:uuid_old``).
315
315
316
316
Legacy UUIDs
317
- ~~~~~~~~~~~~
317
+ ````````````
318
318
319
319
Data stored in BSON::Binary objects of subtype 3 (``:uuid_old``) may be
320
320
persisted in one of three different byte orders depending on which driver
@@ -381,7 +381,7 @@ These methods can be used to convert from one representation to another:
381
381
382
382
383
383
``BSON::Code``
384
- ``````````````
384
+ --------------
385
385
386
386
Represents a string of JavaScript code.
387
387
@@ -390,7 +390,7 @@ Represents a string of JavaScript code.
390
390
BSON::Code.new("this.value = 5;")
391
391
392
392
``BSON::CodeWithScope``
393
- ```````````````````````
393
+ -----------------------
394
394
395
395
.. note::
396
396
@@ -406,7 +406,7 @@ Represents a string of JavaScript code with a hash of values.
406
406
BSON::CodeWithScope.new("this.value = age;", age: 5)
407
407
408
408
``BSON::Document``
409
- ``````````````````
409
+ ------------------
410
410
411
411
This is a subclass of ``Hash`` that stores all keys as strings, but allows
412
412
access to them with symbol keys.
@@ -431,7 +431,7 @@ access to them with symbol keys.
431
431
432
432
433
433
``BSON::MaxKey``
434
- ````````````````
434
+ ----------------
435
435
436
436
Represents a value in BSON that will always compare higher to another value.
437
437
@@ -440,7 +440,7 @@ Represents a value in BSON that will always compare higher to another value.
440
440
BSON::MaxKey.new
441
441
442
442
``BSON::MinKey``
443
- ````````````````
443
+ ----------------
444
444
445
445
Represents a value in BSON that will always compare lower to another value.
446
446
@@ -449,7 +449,7 @@ Represents a value in BSON that will always compare lower to another value.
449
449
BSON::MinKey.new
450
450
451
451
``BSON::ObjectId``
452
- ``````````````````
452
+ ------------------
453
453
454
454
Represents a 12 byte unique identifier for an object on a given machine.
455
455
@@ -458,7 +458,7 @@ Represents a 12 byte unique identifier for an object on a given machine.
458
458
BSON::ObjectId.new
459
459
460
460
``BSON::Timestamp``
461
- ```````````````````
461
+ -------------------
462
462
463
463
Represents a special time with a start and increment value.
464
464
@@ -467,7 +467,7 @@ Represents a special time with a start and increment value.
467
467
BSON::Timestamp.new(5, 30)
468
468
469
469
``BSON::Undefined``
470
- ```````````````````
470
+ -------------------
471
471
472
472
Represents a placeholder for a value that was not provided.
473
473
@@ -476,7 +476,7 @@ Represents a placeholder for a value that was not provided.
476
476
BSON::Undefined.new
477
477
478
478
``BSON::Decimal128``
479
- ````````````````````
479
+ --------------------
480
480
481
481
Represents a 128-bit decimal-based floating-point value capable of emulating
482
482
decimal rounding with exact precision.
@@ -491,7 +491,7 @@ decimal rounding with exact precision.
491
491
BSON::Decimal128.new(d)
492
492
493
493
``Symbol``
494
- ``````````
494
+ ----------
495
495
496
496
The BSON specification defines a symbol type which allows round-tripping
497
497
Ruby ``Symbol`` values (i.e., a Ruby ``Symbol``is encoded into a BSON symbol
@@ -538,7 +538,7 @@ To force encoding of Ruby symbols to BSON symbols, wrap the Ruby symbols in
538
538
# => "\x12\x00\x00\x00\x0Efoo\x00\x04\x00\x00\x00bar\x00\x00"
539
539
540
540
JSON Serialization
541
- ------------------
541
+ ==================
542
542
543
543
Some BSON types have special representations in JSON. These are as follows
544
544
and will be automatically serialized in the form when calling ``to_json`` on
@@ -577,7 +577,7 @@ them.
577
577
578
578
579
579
Time Instances
580
- --------------
580
+ ==============
581
581
582
582
Times in Ruby can have nanosecond precision. Times in BSON (and MongoDB)
583
583
can only have millisecond precision. When Ruby ``Time`` instances are
@@ -610,7 +610,7 @@ calculations may produce unexpected results.
610
610
611
611
612
612
DateTime Instances
613
- ------------------
613
+ ==================
614
614
615
615
BSON only supports storing the time as the number of seconds since the
616
616
Unix epoch. Ruby's ``DateTime`` instances can be serialized to BSON,
@@ -624,7 +624,7 @@ database.
624
624
625
625
626
626
Date Instances
627
- --------------
627
+ ==============
628
628
629
629
BSON only supports storing the time as the number of seconds since the
630
630
Unix epoch. Ruby's ``Date`` instances can be serialized to BSON, but when
@@ -635,7 +635,7 @@ of the day that the ``Date`` refers to in UTC.
635
635
636
636
637
637
Regular Expressions
638
- -------------------
638
+ ===================
639
639
640
640
Ruby regular expressions always have BSON regular expressions' equivalent of
641
641
'm' flag on. In order for behavior to be preserved between the two, the 'm'
@@ -683,7 +683,7 @@ object, one must call ``compile`` on the returned object.
683
683
684
684
685
685
Key Order
686
- ---------
686
+ =========
687
687
688
688
BSON documents preserve the order of keys, because the documents are stored
689
689
as lists of key-value pairs. Hashes in Ruby also preserve key order; thus
@@ -693,7 +693,7 @@ the order of keys in the document will match the order of keys in the hash.
693
693
694
694
695
695
Duplicate Keys
696
- --------------
696
+ ==============
697
697
698
698
BSON specification allows BSON documents to have duplicate keys, because the
699
699
documents are stored as lists of key-value pairs. Applications should refrain
0 commit comments