2
state.db
      .collection(`${collection}_TRENDING`)
      .find(paramsForFind)
      .sort({ timeStamp: -1 })
      .limit(1)
      .toArray();

I don't want to convert to array since it's only one record that this command will return. If I don't use toArray then it throws a cursor object. I just want the data that is returned from the query which backend consumes and throws it to the frontend.

How is it possible? One naive way would be to iterate over the cursor and exit after reading the first and only document. I would prefer a better-engineered way than a for loop.

The module used is:

const MongoClient = require("mongodb").MongoClient;
aviral sanjay
  • 953
  • 2
  • 14
  • 31

0 Answers0