Unclear what exactly the find() method returns and what exactly “result set” and “cursor” mean in that context

Hi tutors and others…

I’m having a little difficulty understanding what exactly the find() method returns and what exactly “result set” and “cursor” mean in that context.

The Cursor lecture says the find method returns a “cursor” which is a pointer to the current location in a “result set”.

So if the find() method returns the cursor/pointer, what’s returning the actual result set that the cursor is pointing at?

Maybe this is just semantics but with a lecture dedicated to it I think I might be missing something. Can anyone explain this further?

Thanks!

1 Like

Hello? Anyone able to offer anything here? Or did I post this wrong somehow?

Please check this

Hi, thanks for the response, and thanks to MongoDB for a great course. It all seems to be very well presented and I’m following everything pretty well I think, so thanks!

Still… I’m not quite clear on this question. That article doesn’t really tell me any more than what the lecture does. I’m sorry to say I’m still not clear on:

“If the find() method returns the cursor/pointer, what’s returning the actual result set that the cursor is pointing at?”

Or… where is the result set stored or returned to or …?

Perhaps to put it another way… It seems there are TWO things that a find somehow produces:

  1. Cursor = returned by the find() method = pointer to result set.
  2. Result set = the thing the cursor is pointing to… but… what exactly is that?
  • What exactly is the result set? Where is it?
  • If the find() method returns the cursor, what’s returning the result set? What’s storing the result set?
  • How does the cursor know what it’s pointing to?
  • When we iterate, how does the cursor know what it’s iterating through?

I’ve searched everywhere I can find and nothing seems to be able to answer those questions. Is there anyone that could explain/clarify this for me please (especially the MongoDB curriculum engineers/instructors/trainers)?

Thanks!

Hi @David_Thorp,

I think you should ask this question in our community forum and someone from the query team will answer these questions.

Hello @David_Thorp,

I did this course early last year, and I too had some questions like that then. I will try to answer some of your questions now.

The result set is the actual documents returned by the query; but is on the database server. In the client program, the mongo shell, you get a cursor (which points or refers or gives access to the result set). You can think the cursor is like an API or a program to work with the result set.

The cursor has many methods which can be run. Some methods affect the result set and some provide the status or info about the result set.

The cursor also maintains some information about the result set. Some information can change as you use the result set data by applying cursor methods.

Cursor has many methods: See Cursor Methods.

You apply these methods and use the returned information or data to suit your application, i.e., what you want to do with the query data in the shell.

Working on the result set using the cursor and its methods:

We will discuss some methods here.

count() returns the count of the number of documents in the result set initially. It is always constant at any point. This is information.

As you read documents from the result set (using cursor’s methods), the result set gets exhausted. Once exhausted you cannot read any more. hasNext() tells if there are any documents available to be read. Returns a boolean true or false. next() returns a document if available (you first check with hasNext, and then do a next).

If you do a toArray(), then all the remaining documents in the result set are loaded into the memory of your client computer and are available as a JavaScript array. The result data is exhausted. hasNext will return false, and next will throw an error.

itcount() returns the count of remaining documents (as a number) in the result set and exhausts it (similar to the toArray).

sort() sorts the documents in the result set.When you fetch the documents using next, or toArray, you get the documents in sorted order.

limit() is used to get the maximum number of documents the cursor can return to you.

Example:

Assume the test collection has 60 documents.

var cur = db.test.find().limit(25) creates a result set from which you can get maximum 25 documents only.

But, cur.count() will show 60, which is the actual count of documents by the query; this is information.

next() will return a document.

itcount() will return 24 (and exhausts the cursor).
itcount() again returns 0.

Finally:

Those are some basic features of cursor and result set. There are many cursor methods, and you can try and see how they work and get a better understanding.

P.S.: Also see: Iterate a Cursor in the mongo Shell

1 Like

Hi @Prasad_45636, and anyone else following along… :wink:

I’m very sorry, I thought I replied to this two weeks ago, but I don’t see it here. It must have not actually saved, or something…

Thank you for the detailed explanation, and I think it answers my question. It might simply be a matter of semantics and terminology. At least from your explanation I certainly understand that the cursor is an object with all those methods, etc, that we can use to operate on the result set in different ways - which is enough to know how to use it practically, which is what matters the most.

That said, I’m going to elaborate on my confusion here in case anyone else is having a similar struggle and this might… and also in case my new understanding is wrong, someone can correct me. :wink:

My confusion is likely coming from my existing understanding of the term “cursor”. eg. Wikipedia defines it as “… a cursor is an indicator used to show the current position for user interaction on a computer … display device that will respond to input from a text input or pointing device.”

So… I’m typing this message - a bunch of text in a box on a web page. The cursor is the flashing line showing me where the next characters I type will appear. But the text itself is a different object. So there’s two things: the indicator, and the text (some part of which the indicator is indicating or pointing to). It could also be a mouse cursor/pointer - the graphical arrow or other image that moves around as the mouse moves, and indicates what any action (eg. clicking the mouse) will act on if acted.

Here in MongoDB (as with any database), the find() function needs to give us some result one way or another. That result needs to be two things: 1. the subset of all the records (which yes, are on the server) that match the find criteria, and 2. some way of working with those records. The instructor in the video says that “the find() function returns a cursor.” Therefore it would seem here the definition of the “cursor” object somehow must include both those things: some definition of the matching subset of records, plus the indicator/pointer used to step through or otherwise act on them.

In short: the “result set” is part of the “cursor” object somehow. At least that’s the only thing that makes any sense to me at this point, though I don’t see it explained that way anywhere.

Again, it’s probably just semantics. I’ve progressed a lot further with this course and two others here in MongoDB University now, and I haven’t found this confusion/distinction to matter in any practical way. Still, maybe my thinking here might be useful feedback for the developers of the MongoDB documentation and/or this course…? :slight_smile:

Thanks again!