Nodejs FindCursor#count deprecation replacement suggestions not producing desired result

I’m working with a very large collection of documents (over 1 million) for testing purposes. I’m using nodejs to access/manipulate the db in order to test the speed for a medium complexity query and I want to know how many documents that query returned so I can forward that to my results log.

I first tried using FindCursor#count, but unfortunately, it is deprecated. In this question and the warning message, collection.countDocuments() or collection.estimatedDocumentCount() are suggested as alternatives to FindCursor#count. Unfortunately, these two methods return a(n estimated) count of all the documents in the entire collection, not the count of the documents that are returned using the query. Or at least that’s what’s happening in my code when I use them.

I’ve tried using collection.countDocuments(cursor) and that doesn’t work (It was a suggested solution on a python forum - not the one here - and even though I’m using nodejs, I thought, why not?).

I’ve tried using cursor.toArray() and because the query is returning over 400,000 documents, it runs out of heap space before it can return the array.

I’m doing this on an M1 mac using mac os Monterey (12.6) with 32 GB of memory and I had nothing else running at the time except I had two mongo shells and vscode open but not doing anything (that I could tell).

Is there some other way I can get the count of just the documents returned by a query? I don’t want to use a deprecated method and I don’t want to have to iterate through the returned FindCursor, mainly because I don’t think I should have to. My expectation is that the FindCursor (or something) should provide a (non-deprecated) method to do so.

I appreciate any help on this.

1 Like

Uhhm honestly i haven’t found a more efficient way to do it than the count() call but here is what i think can be the next best alternative to your large data issue

 const challengeCursor = await db
      .collection("Tasks")
      .aggregate([{ $match: { completed: true } }, { $count: "totalUsers" }])
      .toArray(); 

    if (challengeCursor.length > 0) {
      console.log("Found " + challengeCursor[0].totalUsers + " users");
    } else {
      console.log("No completed tasks found.");
    }```  Though this is a js based approach but i believe you can get the picture it uses the aggeregate method which filters and  creates an array but that array isn't sent over the network .