In the last years I stumbled across a lot of bugs but this one is having me very confused. I tried finding the solution over and over but I can’t find what’s wrong.
I have a MongoDB database and a NodeJS server that I use to query the database. I’m currently trying to make a search API endpoint using $text. I created a text index using mongosh and everything works fine. My search query which is the following works perfectly fine in mongosh:
db.activities.find({ $text: { $search: 'museum' } }).limit(5)
So I naturally tried to implement this in my NodeJS code and I wrote this:
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
},
});
try {
await client.connect();
const database = client.db("favel-test");
const activitiesCollection = database.collection("activities");
const activities = await activitiesCollection
.find({ $text: { $search: "museum" } })
.limit(5)
.toArray();
res.json(activities);
} catch (error) {
console.error("Error occurred during MongoDB query:", error);
res
.status(500)
.json({ error: "An error occurred during the query execution." });
} finally {
await client.close();
}
But then I got this error:
Error occurred during MongoDB query: MongoServerError: error processing query: ns=favel-test.activities limit=5Tree: TEXT : query=musee, language=english, caseSensitive=0, diacriticSensitive=0, tag=NULL
Sort: { mainScore: -1 }
Proj: {}
planner returned error :: caused by :: need exactly one text index for $text query
at Connection.onMessage (/Users/dorian/Documents/Work/Favel/favel-api-v2/node_modules/mongodb/lib/cmap/connection.js:202:26)
at MessageStream.<anonymous> (/Users/dorian/Documents/Work/Favel/favel-api-v2/node_modules/mongodb/lib/cmap/connection.js:61:60)
at MessageStream.emit (node:events:511:28)
at processIncomingData (/Users/dorian/Documents/Work/Favel/favel-api-v2/node_modules/mongodb/lib/cmap/message_stream.js:124:16)
at MessageStream._write (/Users/dorian/Documents/Work/Favel/favel-api-v2/node_modules/mongodb/lib/cmap/message_stream.js:33:9)
at writeOrBuffer (node:internal/streams/writable:399:12)
at _write (node:internal/streams/writable:340:10)
at Writable.write (node:internal/streams/writable:344:10)
at TLSSocket.ondata (node:internal/streams/readable:774:22)
at TLSSocket.emit (node:events:511:28) {
ok: 0,
code: 291,
codeName: 'NoQueryExecutionPlans',
'$clusterTime': {
clusterTime: new Timestamp({ t: 1686696409, i: 10 }),
signature: {
hash: Binary.createFromBase64("A9Ve+C8NJ0e92TFmw8SXCRA8cps=", 0),
keyId: new Long("7185945864957853715")
}
},
operationTime: new Timestamp({ t: 1686696409, i: 10 }),
[Symbol(errorLabels)]: Set(0) {}
}
As far as I understand this error suggests there is not exactly one text index in my collection but I checked multiple times and there is in fact only one text index. I am very confused and I ran out of ideas as to what may cause this error.
Any ideas?