How to speed up Cursor resolve?

Hi there!

I have asked to question already on Stackoverflow but haven’t gotten any answers, yet. So sorry if I’m doing a cross-posting here. But I’d be really glad to receive a feedback on the following problem I have.

So I’m basically trying to fetch an Object field from my document like so:

let cursor = await collection
    .aggregate([
      { $match: { _id: new ObjectId(`${fileId}`) }},
      { $project: { _id: 0, schema: 1}}
    ])
    .toArray()

This approach seems to be very time-consuming because the Object in the ‘schema’ field can be very nested I assume.

Now I have found that this approach (taken from the official documenation):

cursor.stream().on("data", doc => console.log(doc));

prints the schema field very fast in my console, as well as in the right structure that I’d like to have the response.

I’m trying to have this output bound to a variable and doing this in a very complicated way I suppose:

  let schemaCollectionData:any = new Promise((resolve, reject) => {
      const docs: any[] = [];
      cursor.stream().on("data", (doc) => {
        docs.push(doc);
      }).on("end", () => {
          resolve(docs);
      }).on("error", (err) => {
          reject(err);
      });
  });
  schemaCollectionData = await schemaCollectionData

This gives me the desired output bound to “schemaCollectionData” but it takes relatively long comparing to the sole output.

Is there a better and faster way to get the return value of the cursor output and bound it to a variable?

Thanks a lot for your help!

Cheers