@@ -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:
@@ -447,6 +460,57 @@ You can use these methods to convert from one representation to another:
447
460
BSON::Binary.from_uuid('77665544-3322-1100-ffeeddccbbaa9988',:java_legacy).to_uuid(:csharp_legacy)
448
461
# => "33221100-5544-7766-8899aabbccddeeff"
449
462
463
+ .. _ruby-bson-vector:
464
+
465
+ BSON::Vector
466
+ ~~~~~~~~~~~~
467
+
468
+ Starting in ``bson`` v5.1, you can use the
469
+ ``BSON::Vector`` type to represent vectors of numeric values.
470
+
471
+ You can create a ``BSON::Vector`` object to store values of the following
472
+ types:
473
+
474
+ - ``int8``
475
+ - ``float32``
476
+ - ``packed_bit``
477
+
478
+ You can use the optional ``dtype`` and ``padding`` attributes to specify
479
+ the data type of the vector and the bit padding, respectively.
480
+ ``BSON::Vector`` itself is a container to hold your array values, type
481
+ information, and padding specification. ``BSON::Vector`` objects are
482
+ serialized as normal arrays in MongoDB.
483
+
484
+ The following example demonstrates how to create a ``BSON::Vector`` object:
485
+
486
+ .. code-block:: ruby
487
+
488
+ BSON::Vector.new([ -0.0016261312, -0.028070757, -0.011342932 ], :float32)
489
+
490
+ Convert Vectors to Binary
491
+ `````````````````````````
492
+
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:
497
+
498
+ .. code-block:: ruby
499
+
500
+ vector = BSON::Vector.new([ -0.0016261312, -0.028070757, -0.011342932 ], :float32)
501
+ BSON::Binary.from_vector(vector)
502
+
503
+ Using the ``BSON::Binary`` type improves storage efficiency. To learn
504
+ more, see the `BSON specification <https://bsonspec.org/spec.html>`__.
505
+
506
+ You can convert a ``BSON::Binary`` to a ``BSON::Vector`` by using the
507
+ ``BSON::Binary.as_vector`` method.
508
+
509
+ .. tip:: {+avs+}
510
+
511
+ To view an example that leverages the ``BSON::Binary`` type
512
+ to perform efficient {+avs+} queries, see the :ref:`ruby-avs-examples`.
513
+
450
514
BSON::Code
451
515
~~~~~~~~~~
452
516
0 commit comments