Storing Vectors

Declare a field with type: Types.VECTOR and the number of dimensions. Every vector stored in the field must have exactly that many numbers.

const productModel = new Model({
  database: 'shop',
  collection: 'product',
  name: 'Product',
  schema: {
    name: { type: Types.STRING, required: true },
    category: { type: Types.STRING },
    embedding: { type: Types.VECTOR, dimensions: 768, required: true },
  },
});

Field options

OptionValueNotes
dimensionsinteger from 1 to 65536, required Length of every vector. It can't change while the field holds vectors: the server refuses the schema update
required, mutablebooleanAs for other types
dimensions on other types–Refused: the option belongs to VECTOR only
Arrays of vectors–{ type: Types.ARRAY, items: Types.VECTOR } is refused: a document holds one vector per field. Use several fields, or several documents

Insert and update

A vector is a plain array of numbers. An update replaces the whole vector. Vectors come back from find as plain arrays, with exactly the numbers that were stored.

const product = await productModel.insert({ name: 'Trail shoe', category: 'shoes', embedding });

await productModel.update([{ _id: product._id }], { embedding: newEmbedding });

Refused with an error:

  • a vector with another length: property 'embedding' must have 768 dimensions, got 384;
  • items that are not numbers (numeric strings such as "0.5" are converted), NaN or infinity.

How vectors are stored

  • In memory as 8-byte floats: the values are exactly the numbers sent, and each vector takes 8 × dimensions bytes (768 dimensions: about 6 KB).
  • The length of each vector is computed once, when it is stored, so cosine searches don't recompute it.
  • In the dump each vector is one line: f64: followed by the base64 of its floats. That is exact, about half the size of decimal text, and much faster to write and read during snapshots.
  • Changes are in the write-ahead log like any other change.