Hi I have a client side send in a search string and server side code searching a mongodb database for results.
This is my code:
if (req.body.page < 0) req.body.page = 0;
findQuery = {_userId: req.body._userId};
if (req.body.query)
findQuery.name = {"$regex": new RegExp(req.body.query, 'i')};
const numResults = await companySchema.countDocuments(findQuery);
companySchema.find(findQuery).sort({ [req.body.sort]: [req.body.order] }).limit(req.body.results).skip(req.body.page * req.body.results).then(response => {
res.setHeader('Access-Control-Expose-Headers', 'NUMBER-RESULTS')
res.setHeader('NUMBER-RESULTS', numResults);
res.status(200).json( response );
})
}
I'm getting some strange results. For example if I search for "aaa" (i.e. req.body.query is "aaa"), there is one hit and it returns the one result correctly.
Since there are no results for "aaaa" it returns an empty array, and everything functions correctly.
However, if I search for "aaaaa", it returns the entire collection. If I search for "aaaaaa" it returns zero results. And as I continue to add an extra "a" to the end of the search string, it will randomly return either 0 or all the results. It will do this consistently. So "aaaaa" will always return all the entries, and "aaaaaa" will always return 0 results.
The correct behavior should be that it always returns 0 results since anything longer than "aaa" shouldn't produce a hit in the database.
I put in numerous console.log statements and all the req.body entries are correct. Even numResults which comes from the .countDocuments on the same lookup query returns 0 results constantly as it should, but then the same query .find will arbitrarily return all the results sometimes when it should return 0 results.
Any idea what may be causing this?