@@ -294,6 +294,8 @@ In addition to the core {+language+} objects, BSON also provides some special ty
294
294
specific to the specification. The following sections describe other
295
295
types that are supported in the driver.
296
296
297
+ .. _ruby-bson-binary:
298
+
297
299
BSON::Binary
298
300
~~~~~~~~~~~~
299
301
@@ -314,8 +316,19 @@ the bytes encode a particular type of data:
314
316
BSON::Binary.new("binary_string", :user)
315
317
# => <BSON::Binary:0x47113101225420 type=user data=0x62696e6172795f73...>
316
318
317
- The valid subtype specifications are ``:generic``, ``:function``,
318
- ``:old``, ``:uuid_old``, ``:uuid``, ``:md5``, and ``:user``.
319
+ The following list provides the valid subtype specifications:
320
+
321
+ - ``:generic``
322
+ - ``:function``
323
+ - ``:old``
324
+ - ``:uuid_old``
325
+ - ``:uuid``
326
+ - ``:md5``
327
+ - ``:ciphertext``
328
+ - ``:column``
329
+ - ``:sensitive``
330
+ - ``:vector``
331
+ - ``:user``
319
332
320
333
You can use the ``data`` and ``type`` attributes to retrieve a
321
334
``Binary`` object's data and the subtype, as shown in the following code:
@@ -455,20 +468,20 @@ BSON::Vector
455
468
Starting in ``bson`` v5.1, you can use the
456
469
``BSON::Vector`` type to represent vectors of numeric values.
457
470
458
- You can create a ``Vector`` object to store values of the following
471
+ You can create a ``BSON:: Vector`` object to store values of the following
459
472
types:
460
473
461
474
- ``int8``
462
475
- ``float32``
463
476
- ``packed_bit``
464
477
465
- You can use the optional ``dtype`` and ``padding`` atrributes to specify
478
+ You can use the optional ``dtype`` and ``padding`` attributes to specify
466
479
the data type of the vector and the bit padding, respectively.
467
- ``Vector`` itself is a container to hold your array values, type
468
- information, and padding specification. ``Vector`` objects are
480
+ ``BSON:: Vector`` itself is a container to hold your array values, type
481
+ information, and padding specification. ``BSON:: Vector`` objects are
469
482
serialized as normal arrays in MongoDB.
470
483
471
- The following example demonstrates how to create a ``Vector`` object:
484
+ The following example demonstrates how to create a ``BSON:: Vector`` object:
472
485
473
486
.. code-block:: ruby
474
487
@@ -477,23 +490,25 @@ The following example demonstrates how to create a ``Vector`` object:
477
490
Convert Vectors to Binary
478
491
`````````````````````````
479
492
480
- You can convert ``Vector`` and array objects to ``BSON::Binary`` objects
481
- so they are serialized as BSON binary vector (subtype 9) instances. Use
482
- the ``Binary.from_vector`` method, as shown in the following code:
493
+ You can convert ``BSON::Vector`` and array objects to
494
+ :ref:`ruby-bson-binary` objects so they are serialized as BSON binary
495
+ vector (subtype 9) instances. Use the ``BSON::Binary.from_vector``
496
+ method, as shown in the following code:
483
497
484
498
.. code-block:: ruby
499
+
500
+ vector = BSON::Vector.new([ -0.0016261312, -0.028070757, -0.011342932 ], :float32)
501
+ BSON::Binary.from_vector(vector)
485
502
486
- BSON::Binary.from_vector([ -0.0016261312, -0.028070757, -0.011342932 ], :float32)
487
-
488
- Using the ``Binary`` type improves storage efficiency. To learn
503
+ Using the ``BSON::Binary`` type improves storage efficiency. To learn
489
504
more, see the `BSON specification <https://bsonspec.org/spec.html>`__.
490
505
491
- You can convert a ``Binary`` to a ``Vector`` by using the
492
- ``Binary.as_vector`` method.
506
+ You can convert a ``BSON:: Binary`` to a ``BSON:: Vector`` by using the
507
+ ``BSON:: Binary.as_vector`` method.
493
508
494
509
.. tip:: {+avs+}
495
510
496
- To view an example that leverages the ``Binary`` type
511
+ To view an example that leverages the ``BSON:: Binary`` type
497
512
to perform efficient {+avs+} queries, see the :ref:`ruby-avs-examples`.
498
513
499
514
BSON::Code
0 commit comments