Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Mongodb .find returning strange results

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?

like image 276
glog Avatar asked May 13 '26 20:05

glog


1 Answers

Ok I figured out what was wrong, but not sure why. For certain lengths of search string, the line const numResults = await companySchema.countDocuments(findQuery); would modify my findQuery variable. Sometimes it would and sometimes it wouldn't.

I took the recommendation in comments to rewrite the code using aggregate, and then also implemented $facet to count total results and avoid the countDocuments function altogether. This is the new, working code:

          results = await companySchema.aggregate([
                {
                    $match: findQuery
                },
                {
                    $sort: {
                        [req.body.sort]: Number(req.body.order)
                    }
                },
                { $facet: {
                    results: [                
                        {
                            $skip: req.body.page * req.body.results
                        },
                        {
                            $limit: req.body.results
                        }
                    ],
                    totalCount: [ {$count: 'count'}]
                }}
            ], function (err, results) {
                if (err) {
                    es.status(400).json({ message: "COMPANY_SEARCH_ERROR" });
                }
                else {     
                    if (results[0].totalCount[0] == undefined) {
                        returnResults = null;
                        numResults = 0;
                    }
                    else {
                        returnResults = results[0].results;
                        numResults = results[0].totalCount[0].count;
                    }

                    res.setHeader('Access-Control-Expose-Headers', 'NUMBER-RESULTS')
                    res.setHeader('NUMBER-RESULTS', numResults);
                    res.status(200).jsonp( returnResults );
                }
            });```
like image 81
glog Avatar answered May 15 '26 10:05

glog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!