MONGOID-5918 Support BSON::Vector field type - #6162
Merged
comandeo-mongo merged 1 commit intoJul 2, 2026
Merged
Conversation
Declaring a field with type: BSON::Vector now stores the value as a BSON binary of the vector subtype and reads it back as a BSON::Vector, preserving dtype and padding. The extension is guarded so Mongoid still loads on bson-ruby < 5.1, which predates BSON::Vector. Querying by an exact vector value is not supported because BSON::Vector subclasses Array; use $vectorSearch for similarity queries.
comandeo-mongo
marked this pull request as ready for review
July 1, 2026 09:52
Contributor
There was a problem hiding this comment.
Pull request overview
Adds first-class Mongoid field support for BSON::Vector, ensuring vector fields persist as BSON binary vector subtype (9) and are demongoized back to BSON::Vector with dtype/padding preserved (instead of being treated as a plain Array).
Changes:
- Introduces a
Mongoid::Extensions::Vectorextension that mongoizes vectors toBSON::Binary.from_vectorand demongoizes vector binaries back toBSON::Vector. - Wires the new extension into Mongoid’s extension load path.
- Adds RSpec coverage for mongoize/demongoize behavior and persistence round-trips (gated to bson-ruby
>= 5.1).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lib/mongoid/extensions/vector.rb | Adds mongoize/demongoize behavior for BSON::Vector to persist as vector-subtype BSON binary. |
| lib/mongoid/extensions.rb | Requires the new vector extension so it is loaded with other core extensions. |
| spec/mongoid/extensions/vector_spec.rb | Adds unit + integration specs for correct vector persistence and demongoization behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+34
to
+39
| def mongoize(object) | ||
| case object | ||
| when BSON::Vector then BSON::Binary.from_vector(object) | ||
| when BSON::Binary then object | ||
| end | ||
| end |
Comment on lines
+120
to
+131
| context 'when given a vector BSON::Binary' do | ||
| it 'returns it unchanged' do | ||
| binary = BSON::Binary.from_vector(vector) | ||
| expect(BSON::Vector.mongoize(binary)).to eq(binary) | ||
| end | ||
| end | ||
|
|
||
| context 'when given nil' do | ||
| it 'returns nil' do | ||
| expect(BSON::Vector.mongoize(nil)).to be_nil | ||
| end | ||
| end |
jamis
approved these changes
Jul 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
MONGOID-5918
Adds official support for using
BSON::Vectoras a Mongoid field type. A field declaredtype: BSON::Vectoris now stored as a BSON binary of the vector subtype (subtype 9) and read back as aBSON::Vector, preservingdtypeandpadding.Previously this did not work:
BSON::Vectorsubclasses::Array, so without a dedicated extension it was mongoized through theArrayextension and persisted as a plain BSON array, losing the vector subtype.Changes
Implementation:
lib/mongoid/extensions/vector.rb(new) — type-casting forBSON::Vector:#mongoize/.mongoize:BSON::Vector→BSON::Binary.from_vector(vector subtype); a vectorBSON::Binaryis returned unchanged;nilfornil, a plain array, or any other uncastable value..demongoize: a vectorBSON::Binary→BSON::Vector; otherwisenil.if defined?(BSON::Vector)so Mongoid still loads on bson-ruby < 5.1, which predatesBSON::Vector.lib/mongoid/extensions.rb— require the new extension.Tests:
spec/mongoid/extensions/vector_spec.rb(new) — unit coverage formongoize/demongoize(includingnil, non-vector binary, plain array, and uncastable inputs), a regression guard that a vector is not mongoized via theArrayextension, and a persistence round-trip (acrossint8,float32, andpacked_bit) asserting the stored value is a vector-subtype binary that reads back as an equivalentBSON::Vector. Gated withmin_bson_version '5.1'and a guarded model so the bson 4.14/5.0 CI jobs stay green.Scope
Querying by an exact vector value (
where(embedding: vector)) is intentionally not supported. BecauseBSON::VectorsubclassesArray, the criteria selector treats it as a list of elements rather than a scalar, so the value would not match the stored binary. Similarity queries on vector fields use Atlas Vector Search ($vectorSearch), not exact-matchwhere. This is documented in the extension.Test plan
bundle exec rspec spec/mongoid/extensions/vector_spec.rb— 22 examples, 0 failures (live replica set)binary_spec.rb,array_spec.rb,mongoizable_spec.rb— 148 examples, 0 failuresbundle exec rubocop lib/mongoid/extensions/vector.rb lib/mongoid/extensions.rb spec/mongoid/extensions/vector_spec.rb— no offenses