Scheme.find() taking 60 seconds to resolve but executionTimeMillis is 1

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;

    executionStats: {
      executionSuccess: true,
      nReturned: 108,
      executionTimeMillis: 1,
      totalKeysExamined: 0,
      totalDocsExamined: 108,
      executionStages: [Object],
      allPlansExecution: []
    },

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.

Hope you’re having an amazing Christmas :))

Also added executionStages

"executionStages":{"stage":"COLLSCAN","nReturned":108,"executionTimeMillisEstimate":1,"works":110,"advanced":108,"needTime":1,"needYield":0,"saveState":0,"restoreState":0,"isEOF":1,"direction":"forward","docsExamined":108}

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.

See Collection() — MongoDB Node.JS Driver 1.4.9 documentation

Try something like:

const documents = await User.find().toArray() ;

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 :))

Upon looking at the docs, I’ll try changing my code from:

const model = await User.find().toArray();

to:

User.find().toArray((err, docs) => {});

I’ll let you know how it goes!

Nevermind, still appears to say it isn’t a function… could it be because I’m using the mongoose library?

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.

User.find().lean().exec((err, model) => {

It is quite possible. I know nothing about mongoose. I try to shy away from middle ware like this.

Try passing an empty query. User.find( {} ).toArray().

I was told to use mongoose because it’s apparently a nicer wrapper; I’ve also tried to use an empty query but really not sure.

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 :))