Vector Databases and Knowledge Storage
🎯 Introduction
In “Build Your First RAG Application,” we stored passage vectors in memory. They disappeared when the program exited and had to be regenerated on the next run. In the previous lesson, “Loading and Chunking Documents,” we turned Markdown into chunks containing text and source information.
Next, we need to persist those chunk vectors. But MySQL and Redis can store data too, so why use a vector database? We will first distinguish vector databases from ordinary data storage, then write a chunk to Qdrant, read it back, and check persistence. Similarity search using a user’s question comes in the next article.
🧱 A Vector Database Does More Than Store JSON
A record in Qdrant is called a point:
{
id: "fb1359e8-82d1-4c7a-9e67-eeabe6fc47bc",
vector: [0.12, 0.35, -0.08],
payload: {
text: "You can request a refund within 7 days of purchase.",
source: "guide.md"
}
}
This three-dimensional vector illustrates the structure; it is not output from a real embedding model.
Think of it as a record whose ID associates a vector with application data, with more to it than an ID mapped to JSON:
ididentifies the record for reads or updates by ID.vectoris the numeric representation of the text, used for similarity search.payloadcontains associated application data as JSON, such as the original text and source.
Qdrant organizes points into collections. We will create a collection called rag_docs to store knowledge chunks.
Can’t MySQL and Redis Store This Data?
They can. You could store an id, vector array, and original text in a MySQL table or in Redis. For small datasets, an application could even keep them in an ordinary array and calculate similarity for each entry, as our minimal RAG example did.
The key distinction is that storing data and efficiently finding semantically similar material are different tasks.
Suppose the knowledge base contains 100,000 passages and a user asks, “Can I return something after buying it?”
- Lookup by ID: If you already know a passage’s ID, the database can locate it directly. The user, however, does not know the refund policy’s ID.
- Lookup by vector similarity: Convert the question into a vector, then find the closest document vectors. This can locate the refund policy even when the question does not use the same keywords as the source.
If vectors are stored as ordinary arrays, a straightforward approach is to calculate similarity for every record and sort the results. That becomes more expensive as the dataset grows. Qdrant provides indexes and queries designed for vector similarity search, so we do not have to iterate over every record in application code.
We will use Qdrant to learn the storage structure here, then use user questions for vector search in the next article.
🛠 Start Qdrant
These examples use Node.js 24.x, Qdrant 1.20.0 in Docker, openai@7.62.0, and @qdrant/js-client-rest@1.20.0.
Start Qdrant from your project directory:
docker run -d --name rag-qdrant -p 127.0.0.1:6333:6333 -v "$(pwd)/qdrant_storage:/qdrant/storage" qdrant/qdrant:v1.20.0
Qdrant persists data to disk. The Docker mount stores it in the project’s qdrant_storage directory so it is not confined to the container. Once started, open http://localhost:6333/dashboard to view the Qdrant Dashboard.
docker start rag-qdrant as needed.Install the JavaScript dependencies:
npm install --save-exact openai@7.62.0 @qdrant/js-client-rest@1.20.0
Continue using AI_API_KEY, AI_BASE_URL, and AI_EMBEDDING_MODEL from the earlier lessons’ .env file. Confirm that your configured model service supports the Embeddings API; we will not repeat key setup here.
🧩 Prepare a Chunk to Store
We already covered Markdown chunking, so we will use one of those chunks directly without repeating the chunking code.
Create 04.storage.mjs and initialize the clients and data:
import OpenAI from "openai";
import { QdrantClient } from "@qdrant/js-client-rest";
const ai = new OpenAI({
apiKey: process.env.AI_API_KEY,
baseURL: process.env.AI_BASE_URL
});
const qdrant = new QdrantClient({ url: "http://127.0.0.1:6333" });
const collection = "rag_docs";
const pointId = "fb1359e8-82d1-4c7a-9e67-eeabe6fc47bc";
const chunk = {
text: "Refunds\nYou can request a refund within 7 days of purchase. Sale items are nonrefundable.",
source: "guide.md",
heading: "Refunds"
};
chunk is a text chunk from the previous lesson. pointId is a pregenerated UUID, kept fixed here so we can read the same record and verify updates later. Assign separate IDs when storing different chunks.
🗃 Create a Collection
Append this to 04.storage.mjs:
const result = await ai.embeddings.create({
model: process.env.AI_EMBEDDING_MODEL,
input: chunk.text
});
const vector = result.data[0].embedding;
const existing = await qdrant.getCollections();
if (!existing.collections.some((item) => item.name === collection)) {
await qdrant.createCollection(collection, {
vectors: {
size: vector.length,
distance: "Cosine"
}
});
}
This generates a vector for chunk.text, then creates a collection to store it. If the collection already exists, creation is skipped.
When creating a collection, specify:
size: The number of values in each vector. This must match the length returned by the embedding model.distance: The method used to compare vectors. We useCosine; cosine similarity was covered earlier.
📥 Write a Point
Continue adding:
await qdrant.upsert(collection, {
wait: true,
points: [{
id: pointId,
vector,
payload: chunk
}]
});
console.log("Write complete. Point ID:", pointId);
console.log("Vector dimensions:", vector.length);
In this example, each chunk corresponds to one point. vector stores the embedding generated from chunk.text; payload stores the chunk’s text, filename, and heading; and id identifies the record. upsert() writes them to the collection, and wait: true waits for the server to finish the update.
Run:
node --env-file=.env 04.storage.mjs
Example output is shown below. The actual vector dimensions depend on your embedding model:
Write complete. Point ID: fb1359e8-82d1-4c7a-9e67-eeabe6fc47bc
Vector dimensions: 1536
So far, we have saved a record under a specified ID; we have not implemented semantic search. First, let’s verify that we can read the data back.
🔍 Read and Verify the Stored Data
Create 04.read.mjs. It retrieves the point directly by ID without regenerating its vector:
import { QdrantClient } from "@qdrant/js-client-rest";
const qdrant = new QdrantClient({ url: "http://127.0.0.1:6333" });
const pointId = "fb1359e8-82d1-4c7a-9e67-eeabe6fc47bc";
const points = await qdrant.retrieve("rag_docs", {
ids: [pointId],
with_payload: true,
with_vector: false
});
console.log(points);
with_payload: true returns the stored application data, while with_vector: false omits the long vector array. Run:
node 04.read.mjs
The returned data has roughly this structure (this is a formatted illustration, not a verbatim terminal capture):
[
{
id: "fb1359e8-82d1-4c7a-9e67-eeabe6fc47bc",
payload: {
text: "Refunds\nYou can request a refund within 7 days of purchase. Sale items are nonrefundable.",
source: "guide.md",
heading: "Refunds"
}
}
]
You can also open rag_docs in the Qdrant Dashboard to inspect the record.
Can You Still Read It After a Restart?
Run:
docker stop rag-qdrant
docker start rag-qdrant
After the service is ready again, run only the read script:
node 04.read.mjs
If you can still retrieve the same point, you have verified that the data survives stopping and restarting the same container. Qdrant stores data on disk, and the directory mount places it in qdrant_storage on the host. This procedure does not test deleting and recreating the container.
For demonstration purposes, 04.storage.mjs still calls the Embeddings API on every run. Real projects generally update only the vectors for new or modified documents. Queries use the existing database without regenerating all document vectors.
❓ Knowledge Check
Q: What happens when upsert() encounters an existing point ID?
It updates the record with that ID instead of inserting another record with the same ID. In the exercise below, keep pointId unchanged, modify the chunk content, and read it back to verify the update.
🧪 Exercise
In 04.storage.mjs, change the refund window from “7 days” to “10 days.” Rerun the write script, then run 04.read.mjs.
Success criteria: The retrieved point ID stays the same, the refund window in payload.text becomes “10 days,” and the operation does not add another record with a different ID to the collection.
🎉 Congratulations! You now understand the key distinction between vector databases and ordinary data storage, and can create collections, write and read points, and verify persistence with Qdrant.
