Our React app has a live search that is connected to a MongoDB collection with the names of 250K individuals. On key stroke, the Live Search uses regex to grab the top 20 documents in the collection. Our data fetching route for the Live Search is straightforward and looks like this:
router.get('/live-search/text/:text', function (req, res) {
let text = req.params.text;
let queryFilters = { label: { $regex: `${text}`, $options: 'i' } };
db.gs__ptgc_selects.find(queryFilters).limit(20)
.then(data => res.json(data))
.catch(err => res.status(400).json('Error: ' + err))
});
I am certain that our collection has the text index, and it is set on the label
column. Here is a screenshot of our MongoDB Atlas page showing as such:
The problem is that the index does not appear to be working. Whenever our app's live search is used (which is all the time), I continue to get email warnings for Query Targeting: Scanned Objects / Returned has gone above 1000
. I've checked the MongoDB Atlas Profiler for these queries, and (I think) confirmed that the index is not being used:
Am I missing something? It's hard to create a fully reproducible example out of this. We've been struggling with the MongoDB text index and using it with our Live Search.
Edit
Maybe I need to be using $search
, like it's done here: https://docs.mongodb.com/manual/text-search/. I am currently string matching using regex
, but am not using mongo's $search
.