Basic Reads - Where is the content of the read operation

Hello,

So, I believe this a lack of understanding on my part, since I am not seeing what I would expect.

This is from basic reads section of the course we have this piece of code.

it("all", async () => {
  let result = await movies.find({
      cast: { $all: ["Salma Hayek", "Johnny Depp"] },
  })

  // My piece of code
  console.log(result)

  let { title, year, cast } = await result.next()

  console.log({ title, year, cast })
})

The console log of result, contains query statistics, not the results of the query, which is what I would expect.

Yet, the let { title, year, cast } = await result.next() which follows, pulls data from the result.next(), where I would have expected the console.log(result) to have shown that same data as well as the rest of the data from that document.

To be honest I listened to that section a couple of times, and it doesn’t get explained, and the student seems to have to take it on trust. Does it get explained properly later?

What is the flow of execution, if that query had pull back multiple documents?

Is it the .next() which performs the actual iteration over the results set?

Thanks.

PS - Yet the findOne() works more as I would expect, with regards to what result would contain at what start.

Hi @NeilM, :wave:

Please note that result is the cursor and not the query result.
Learn more about cursor here.

As mentioned in the lecture:

The result.next method will return the next result in the cursor.
If there are no more results, next() resolves to null.

This is the expected behaviour of the result object.

Yes, absolutely correct.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

So the next(), grabs the first document and each following document, stepping through the result set, which the cursor result is pointing to.

I had read the notes, but hadn’t really visualized what was happening with each command.

Thank you.

PS - the list of cursor commands, makes things clearer, since I wasn’t sure whether next() was a javascript instruction or a mongodb command/method.

It could be worth adding in the notes section under the video, a note maybe with a link to cursors, pointing out that next() for instance is a method of cursor.

1 Like

Correct.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer