I developped a text search with mongodb and nodejs for a school project but i got a problem with it. I have in my database a place with the name "L'Atomium" but when i search "Atomium",mongodb doesn't find it,is there anyway that he does? My db looks like this:
{
"name": "",
"coordinate": "",
"rating": 0,
"commentaries": "",
"description": ""
}
and my code for the text search looks something like this:
const dbo = db.db('mydb');
dbo.collection("places").createIndex({name: "text"}).then(r => {
dbo.collection("places").find({"$text": {"$search": wordlist,"$caseSensitive": false,
"$diacriticSensitive": false }}).toArray((err,placelist) => {
*rest of the code*
Sorry if the question is not clear,i'm a total beginner with Stack Overflow. Thank you!
It actually isn't possible to find suffixes with text indices in MongoDB this way. You may be able to try something like
dbo.collection("places").find({$or: [{$text: {$search: "Atomium"}}, {foo: {$regex: "^Atomium"}}]})
However, the performance and results compiled from such a query will still not yield the best possible outcome.
If you are hosting your MongoDB database with MongoDB Atlas and you want full-text search capabilities, look into $search with Atlas https://docs.atlas.mongodb.com/atlas-search/. Its scoring and search capabilities are far beyond that of $text search.
A query using Atlas $search might look something like.
dbo.collection("places").aggregate([
{
$search: {
query: "Atomium",
path: "name",
fuzzy: { maxEdits: 2 }
}
}
]);