Saturday, 18 April 2020

Unstable MongoDB Query Time with Hard to Debug Situation

I have a code with mongoose.js to query the mongoDB. The collection has around 20k documents.console.time("query"); collection.findOne({"userUUID": userUUID}, function (err, user) { console.timeEnd("query"); //other code... } The problem is, the query time is quite unstable. As shown by console.time, the time ranges from few milliseconds, to several second. I am not sure if this is due to Node.js itself, or due to MongoDB.I am more inclined to say it's MongoDB issue. Because as the query is being processed, Node.js still manage to fire websocket data to my front-end, thus from what I can see, the Node.js main thread isn't blocked.But then, I have already index the "userUUID" for this collections. When I run the db.collection.find({"userUUID": userUUID}).explain("executionStats") in mongo shell, it shows that execution status is "IXSCAN", which means MongoDB uses index to perform a scan. Moreover, the "executionTimeMillis" is 25, and "totalDocsExamined" is only 1. So it looks like MongoDB doesn't have issue as well.I am not sure where to look for after seeing that Node.js and MongoDB seemingly have no issue on their part. And the occurrence of the problem is unstable, as mentioned above, the query sometimes is very fast with millisecond, but sometimes it takes a few seconds.

Submitted April 18, 2020 at 08:52AM by VickNicks

No comments:

Post a Comment