Skip to content

Commit 17eeb68

Browse files
authored
DOCSP-49237: bson vector subtype (#152)
* DOCSP-49237: bson vector subtype * Use vector in avs example * update to accurate info about vector/binary * DR + RM PR comments * small fix
1 parent 97876e6 commit 17eeb68

File tree

5 files changed

+81
-6
lines changed

5 files changed

+81
-6
lines changed

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ patch-version-number = "{+version-number+}.0"
2727
version = "v{+version-number+}"
2828
stable-api = "Stable API"
2929
api-root = "https://www.mongodb.com/docs/ruby-driver/current/api/"
30-
bson-version = "5.0.2"
30+
bson-version = "5.1.0"
3131
avs = "Atlas Vector Search"

source/data-formats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ application. To learn how to work with these data formats, see the following
3333
sections:
3434

3535
- :ref:`ruby-time-series`: Learn how to create a time series collection and interact with time series data.
36-
- :ref:`ruby-bson-tutorial`: Learn how to work with BSON data by using the {+driver-short+}'s BSON library.
36+
- :ref:`ruby-bson`: Learn how to work with BSON data by using the {+driver-short+}'s BSON library.

source/data-formats/bson.txt

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ In addition to the core {+language+} objects, BSON also provides some special ty
294294
specific to the specification. The following sections describe other
295295
types that are supported in the driver.
296296

297+
.. _ruby-bson-binary:
298+
297299
BSON::Binary
298300
~~~~~~~~~~~~
299301

@@ -314,8 +316,19 @@ the bytes encode a particular type of data:
314316
BSON::Binary.new("binary_string", :user)
315317
# => <BSON::Binary:0x47113101225420 type=user data=0x62696e6172795f73...>
316318

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``
319332

320333
You can use the ``data`` and ``type`` attributes to retrieve a
321334
``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:
447460
BSON::Binary.from_uuid('77665544-3322-1100-ffeeddccbbaa9988',:java_legacy).to_uuid(:csharp_legacy)
448461
# => "33221100-5544-7766-8899aabbccddeeff"
449462

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+
450514
BSON::Code
451515
~~~~~~~~~~
452516

0 commit comments

Comments
 (0)