I’m using the module Mongoose on a small database hosted on Atlas with around 110~ documents (108 to write). I’ve been having issues when having to fetch the entire database to perform some statistical information (pretty rare that the query to fetch all is ran because I know it can be impacting on performance.)
The code is something along the lines of this (User is a Schema):
var before = Date.now();
const data = await User.find();
console.log(Date.now() - before); // Between 60-100 seconds
I decided to run explain to try and see what was going on but I received extremely conflicting data to what’s shown above;
The total documents examined and returned are correct and fine, but it only took one millisecond to execute?! I would like to know if anyone has any advice on what I should do because I really have no idea how to debug this. I can provide more information if necessary.
Most likely it takes that long because you do not consume the data from the cursor created by user.find(). The delay is probably the cursor expiring in the server. If have to call something like toArray() to get the data in your application.
Thanks for the clarification on the cursor part! I’ve just added toArray() on the end but it seems to be saying it’s not a function of find? Again thank you for responding :))
It appears toArray is not a function in mongoose, so I’ve switched to the code below but it is still as slow as before. Really not sure what to do anymore.
Hopefully, a mongoose user will chip in to help you further. Try your query in mongo shell or mongosh and this will give you an idea of the speed you should get. Make sure you call toArray() to consume the documents from the cursor.
I am aware there is a ‘cursor’ feature in mongoose but apparently it isn’t a real mongodb cursor. And yeah, hopefully someone else can respond but thank you so much for all help you have provided so far :))