Maybe the createIndex function could return some error message
This is my creating index code.
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db('admin').command({ ping: 1 });
console.log('Connected successfully to server');
const database = client.db("parse")
const collection = database.collection("TblColllection")
await collection.createIndex({ tokenURIData: "text" })
console.log('Created index successfully');
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
Try to list the indexes for that table that are found in the database
It returns this value.
tokenURIData_text
Should I need to use this return value like this?
query.fullText('tokenURIData_text', 'xxx')
How can I list indexes?
Could you tell me how to list indexes?
I listed indexes.
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { _fts: 'text', _ftsx: 1 },
name: 'tokenURIData_text',
weights: { tokenURIData: 1 },
default_language: 'english',
language_override: 'language',
textIndexVersion: 3
}
]
Do you still get an error?
Yeah, it still occurs exception.
{"ok":0,"code":27,"codeName":"IndexNotFound","name":"MongoError"}
Did you try the exact syntax used here for the query?
This is my creating index code.
await client.db('admin').command({ ping: 1 });
console.log('Connected successfully to server');
const database = client.db("parse")
const collection = database.collection("TblColllection")
const res = await collection.createIndex({ tokenURIData: "text" })
So after this code, I listed the indexes and result is like below.
[
{ v: 2, key: { _id: 1 }, name: '_id_' },
{
v: 2,
key: { _fts: 'text', _ftsx: 1 },
name: 'tokenURIData_text',
weights: { tokenURIData: 1 },
default_language: 'english',
language_override: 'language',
textIndexVersion: 3
}
]
And I used query.fullText method on cloud function as below.
query.fullText('tokenURIData', request.params.keyword);
The original code was also doing a query after creating the index
Yeah, right. But the code was on client side.
But I need to implement this feature on cloud functions.
yes, just wanted to know if it works that code in client side that was used there
I added this code on client side and it is also not working.
const datas = await collection.aggregate([{ $match: { $text: { $search: "test" } } }]).toArray();
console.log(datas);
And there’s a record which tokenURIData contains ‘test’, but it returns empty array.
original code
await client.connect()
const database = client.db("parse")
const collection = database.collection("itemCreatedData")
await collection.createIndex({ title: "text" })
const datas = await collection.aggregate([{ $match: { $text: { $search: "queen" } } }]).toArray()
new code:
await client.db('admin').command({ ping: 1 });
console.log('Connected successfully to server');
const database = client.db("parse")
const collection = database.collection("TblColllection")
const res = await collection.createIndex({ tokenURIData: "text" })
const datas = await collection.aggregate([{ $match: { $text: { $search: "test" } } }]).toArray();
console.log(datas);
seems similar
maybe you can also try this: const datas = await collection.aggregate([{ $match: { $tokenURIData_text: { $search: "test" } } }]).toArray();
I think that you already tried it.
It may be easier to use a regexp
Yeah, I tried and it works the same.
regexp part doesn’t work either?
When I use regexp on normal javascript, we don’t use quote like below.
regexp = /test/g;
But when I use this format on cloud function, it returns an error and requires regexp as string.
So I used ‘/test/g’, but in my opinion, in this case, it is searching ‘/test/g’ string not regexp string.
And I tried with this code and in this case it occurs exception but exception is an empty object.
pipelin.push({
match: {
tokenURIData: { $regex: `/${request.params.keyword}/g` },
},
})
regexp should work without issues, try with simpler queries maybe, hardcoded values