What dose it mean by exhausts the cursor?

hi i reading in the documentation about https://www.mongodb.com/docs/manual/tutorial/iterate-a-cursor/
this is talking about cursor but what i don’t know what is the meaning of cursor is exhausted please i need an explanation with example to understand because i am begginner

Hello :wave: @mina_remon,

Welcome back to the MongoDB Community forums :sparkles:

In this context, the term “exhausted” refers to a cursor that has been fully traversed and has no more data to return.

A cursor is a pointer to the result set of a query in MongoDB. When a query is executed, MongoDB returns a cursor that can be used to iterate over the results. Cursors are often used for large result sets or when results need to be processed in batches.

So, when a client has exhausted a cursor, it means that the client has retrieved all the documents that match the query conditions and there are no more documents left to fetch. At this point, the cursor can be closed and resources can be freed up.

For example, imagine a collection of customer orders in a MongoDB database. If a query is executed to retrieve all orders for a particular customer, a cursor will be returned with all matching orders. If the cursor is iterated over and all orders are retrieved, the cursor will be exhausted and can be closed.

Further, in the docs, it describes the cursor behavior:

In MongoDB 5.0 and 4.4.8, cursors created within a client session will automatically close when the cursor is exhausted, and the corresponding server session ends with the killSessions command or the session times out. However, if it is created outside the session it will automatically close after 10 minutes of inactivity, or if the client has exhausted the cursor.

I hope it answers your question. Let us know if you have any further questions.

Regards,
Kushagra

1 Like

@Kushagra_Kesav Hi when I did a while loop or foreach loop sometimes I got a message that say cursor exhausted and I got to collection is that a different situation

Hi @mina_remon,

Here, I tried to reproduce the error using the mongo shell.

This is the sample collection named test which contains one document as follows:

{
  "_id": {
    "$oid": "642e640ebba9b652048e9be3"
  },
  "timestamp": 1681110227585,
  "measure": "12"
}

Here I’m iterating through the test collection and printing the measure field for each document using the forEach method on the cursor returned by the find:

ts> var cursor = db.test.find()
ts> cursor.forEach(function(ts) {print(ts.measure)})
12
ts> cursor.forEach(function(ts) {print(ts.measure)})
MongoCursorExhaustedError: Cursor is exhausted

The first loop completes successfully and prints the measure for a document in the collection. However, when I tried to run the loop again, we get a “MongoCursorExhaustedError: Cursor is exhausted” error. This error occurs because the cursor has been exhausted and does not contain any more data to retrieve. :raised_hands:

Can you please elaborate on what you mean by the above statement? If possible please provide any example to help us further understand the issue.

Best,
Kushagra

1 Like

no problem consider it a wrong statment that i wrote by mistack what i want to know is why i got the
MongoCursorExhaustedError: Cursor is exhausted ” and you gave me the answer

1 Like

ok i tried this in one session it works for the first time and loops over the cursor and gave me the documents then on the same session i tried it again and gave me “MongoCursorExhaustedError: Cursor is exhausted ” error but when i opned a new session on the linux terminal i tried the same statement it works again and gave me the results
1-first session terminal one window
Screenshot from 2023-04-13 05-24-43

2- another diffrent session terminal 2:
Screenshot from 2023-04-13 05-26-37

Hello @mina_remon,

Yes, it’s obvious that you obtained the result by repeating the same operation in the new session. Could you please help me understand what your question is?

Best,
Kushagra

There are no questions.
You answered me. Thank you very much