Skip to content

MONGOID-5918 Support BSON::Vector field type - #6162

Merged
comandeo-mongo merged 1 commit into
mongodb:masterfrom
comandeo-mongo:MONGOID-5918-bson-vector
Jul 2, 2026
Merged

MONGOID-5918 Support BSON::Vector field type#6162
comandeo-mongo merged 1 commit into
mongodb:masterfrom
comandeo-mongo:MONGOID-5918-bson-vector

Conversation

@comandeo-mongo

Copy link
Copy Markdown
Contributor

MONGOID-5918

Adds official support for using BSON::Vector as a Mongoid field type. A field declared type: BSON::Vector is now stored as a BSON binary of the vector subtype (subtype 9) and read back as a BSON::Vector, preserving dtype and padding.

Previously this did not work: BSON::Vector subclasses ::Array, so without a dedicated extension it was mongoized through the Array extension and persisted as a plain BSON array, losing the vector subtype.

Changes

Implementation:

  • lib/mongoid/extensions/vector.rb (new) — type-casting for BSON::Vector:
    • #mongoize / .mongoize: BSON::VectorBSON::Binary.from_vector (vector subtype); a vector BSON::Binary is returned unchanged; nil for nil, a plain array, or any other uncastable value.
    • .demongoize: a vector BSON::BinaryBSON::Vector; otherwise nil.
    • Guarded with if defined?(BSON::Vector) so Mongoid still loads on bson-ruby < 5.1, which predates BSON::Vector.
  • lib/mongoid/extensions.rb — require the new extension.

Tests:

  • spec/mongoid/extensions/vector_spec.rb (new) — unit coverage for mongoize/demongoize (including nil, non-vector binary, plain array, and uncastable inputs), a regression guard that a vector is not mongoized via the Array extension, and a persistence round-trip (across int8, float32, and packed_bit) asserting the stored value is a vector-subtype binary that reads back as an equivalent BSON::Vector. Gated with min_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. Because BSON::Vector subclasses Array, 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-match where. This is documented in the extension.

Test plan

  • bundle exec rspec spec/mongoid/extensions/vector_spec.rb — 22 examples, 0 failures (live replica set)
  • Regression: binary_spec.rb, array_spec.rb, mongoizable_spec.rb — 148 examples, 0 failures
  • bundle exec rubocop lib/mongoid/extensions/vector.rb lib/mongoid/extensions.rb spec/mongoid/extensions/vector_spec.rb — no offenses

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 comandeo-mongo added the feature Adds a new feature, without breaking compatibility label Jun 30, 2026
@comandeo-mongo
comandeo-mongo marked this pull request as ready for review July 1, 2026 09:52
@comandeo-mongo
comandeo-mongo requested a review from a team as a code owner July 1, 2026 09:52
@comandeo-mongo
comandeo-mongo requested review from Copilot and jamis July 1, 2026 09:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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::Vector extension that mongoizes vectors to BSON::Binary.from_vector and demongoizes vector binaries back to BSON::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
@comandeo-mongo
comandeo-mongo merged commit 4b9edba into mongodb:master Jul 2, 2026
76 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Adds a new feature, without breaking compatibility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants