// $vectorSearch from the official Node.js driver. Reads the shared query vectors and compares the top-5 ids with pymongo. import { MongoClient } from "mongodb"; import { readFileSync } from "node:fs"; const uri = process.env.MONGODB_URI ?? "mongodb://mongo:27017/?directConnection=true"; const qvecs = JSON.parse(readFileSync("/work/qvecs.json", "utf8")); const py = JSON.parse(readFileSync("/work/top5_pymongo.json", "utf8")); const client = new MongoClient(uri); await client.connect(); const col = client.db("kb").collection("chunks"); console.log("driver mongodb 7.8.0, node", process.version); let same = 0; for (const [q, vec] of Object.entries(qvecs)) { const docs = await col .aggregate([ { $vectorSearch: { index: "kb_vec", path: "embedding", queryVector: vec, numCandidates: 100, limit: 5 } }, { $project: { url: 1, score: { $meta: "vectorSearchScore" } } }, ]) .toArray(); const ids = docs.map((d) => d._id); const key = q.replace(/^search_query: /, ""); const ok = JSON.stringify(ids) === JSON.stringify(py[key]); same += ok; console.log(`${ok ? "same" : "DIFF"} ${key.padEnd(72)} top hit ${docs[0].url.replace("https://webcrawlerapi.com", "")} ${docs[0].score.toFixed(4)}`); } console.log(`top-5 ids identical to pymongo (same order) on ${same} of ${Object.keys(qvecs).length} queries`); await client.close();