MongoDB : Why should we close the cursor after it is used? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Does MongoCursor always need explictly closing?Cursor leak in MongoDBCreate a list from DbCursor in MongoDB and JavaMongoDB or CouchDB - fit for production?MongoDB vs. CassandraNoSQL (MongoDB) vs Lucene (or Solr) as your databaseHow to query MongoDB with “like”?What did MongoDB not being ACID compliant before v4 really mean?How do I drop a MongoDB database from the command line?Why shouldn't I use mysql_* functions in PHP?“Large data” work flows using pandasWhat is a Cursor in MongoDB?Close a MongoDB Tailable Cursor in JAVA

Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?

Like totally amazing interchangeable sister outfit accessory swapping or whatever

What is the ongoing value of the Kanban board to the developers as opposed to management

When does Bran Stark remember Jamie pushing him?

Etymology of 見舞い

Can the van der Waals coefficients be negative in the van der Waals equation for real gases?

How to ask rejected full-time candidates to apply to teach individual courses?

How can I introduce the names of fantasy creatures to the reader?

Is "ein Herz wie das meine" an antiquated or colloquial use of the possesive pronoun?

Coin Game with infinite paradox

Protagonist's race is hidden - should I reveal it?

Why doesn't the university give past final exams' answers?

Short story about an alien named Ushtu(?) coming from a future Earth, when ours was destroyed by a nuclear explosion

Who's this lady in the war room?

What is her name?

Why isn't everyone flabbergasted about Bran's "gift"?

Can I take recommendation from someone I met at a conference?

Does the universe have a fixed centre of mass?

How is an IPA symbol that lacks a name (e.g. ɲ) called?

What helicopter has the most rotor blades?

A German immigrant ancestor has a "Registration Affidavit of Alien Enemy" on file. What does that mean exactly?

Compiling and throwing simple dynamic exceptions at runtime for JVM

lm and glm function in R

Trying to enter the Fox's den



MongoDB : Why should we close the cursor after it is used?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!Does MongoCursor always need explictly closing?Cursor leak in MongoDBCreate a list from DbCursor in MongoDB and JavaMongoDB or CouchDB - fit for production?MongoDB vs. CassandraNoSQL (MongoDB) vs Lucene (or Solr) as your databaseHow to query MongoDB with “like”?What did MongoDB not being ACID compliant before v4 really mean?How do I drop a MongoDB database from the command line?Why shouldn't I use mysql_* functions in PHP?“Large data” work flows using pandasWhat is a Cursor in MongoDB?Close a MongoDB Tailable Cursor in JAVA



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I have seen that people close the cursor after it has been used. I also read in documentation that server closes the cursor after 10 minutes of inactivity.



I searched the net but didn't find proper answer. I am new to both database and MongoDB.



Why is it necessary to close the cursor?










share|improve this question






























    4















    I have seen that people close the cursor after it has been used. I also read in documentation that server closes the cursor after 10 minutes of inactivity.



    I searched the net but didn't find proper answer. I am new to both database and MongoDB.



    Why is it necessary to close the cursor?










    share|improve this question


























      4












      4








      4


      3






      I have seen that people close the cursor after it has been used. I also read in documentation that server closes the cursor after 10 minutes of inactivity.



      I searched the net but didn't find proper answer. I am new to both database and MongoDB.



      Why is it necessary to close the cursor?










      share|improve this question
















      I have seen that people close the cursor after it has been used. I also read in documentation that server closes the cursor after 10 minutes of inactivity.



      I searched the net but didn't find proper answer. I am new to both database and MongoDB.



      Why is it necessary to close the cursor?







      mongodb memory-leaks cursor mongodb-query database






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 22 '17 at 17:57









      Community

      11




      11










      asked Jun 17 '14 at 9:50









      Pratik PatelPratik Patel

      7311133




      7311133






















          2 Answers
          2






          active

          oldest

          votes


















          14














          Closing the cursor is only really required when you do not "exhaust" the results. Or in other terms, iterate over all the possible results returned by the cursor.



          Leaving a "cursor" open is like leaving an open connection that never gets re-used. These things are not free. In fact the standard connection cost is 1MB (approx). So if you are leaving a lot of "partially iterated" cursors hanging around there is a general overhead in terms of an active connection and it's memory usage.



          If in fact you actually always iterate "all" of the results (and that includes a "limit" which is a "cursor modifier") then the cursor will close and all is okay.



          General usage will be that you actually exhaust/deplete the cursor by going through all of the results. Therefore there is no explicit need to destroy.






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49


















          3














          It depends on your usage, but at least in my web application - the client handles the closing.
          Why? because my web application follows the pattern of short & stateless request handling (you get a request from the browser, build an HTTP response quickly - less than a second - and this response relies on mongo data). So my client only needs the connection for 1 second.



          Now, consider what if I have (say) 50 requests per minute... my server handles them comfortably. But it would have crashed if each request were to hold on to resources for 10 minutes... E.g. after 9 minutes I would have 450 unnecessary open resources...






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49











          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24260677%2fmongodb-why-should-we-close-the-cursor-after-it-is-used%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          14














          Closing the cursor is only really required when you do not "exhaust" the results. Or in other terms, iterate over all the possible results returned by the cursor.



          Leaving a "cursor" open is like leaving an open connection that never gets re-used. These things are not free. In fact the standard connection cost is 1MB (approx). So if you are leaving a lot of "partially iterated" cursors hanging around there is a general overhead in terms of an active connection and it's memory usage.



          If in fact you actually always iterate "all" of the results (and that includes a "limit" which is a "cursor modifier") then the cursor will close and all is okay.



          General usage will be that you actually exhaust/deplete the cursor by going through all of the results. Therefore there is no explicit need to destroy.






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49















          14














          Closing the cursor is only really required when you do not "exhaust" the results. Or in other terms, iterate over all the possible results returned by the cursor.



          Leaving a "cursor" open is like leaving an open connection that never gets re-used. These things are not free. In fact the standard connection cost is 1MB (approx). So if you are leaving a lot of "partially iterated" cursors hanging around there is a general overhead in terms of an active connection and it's memory usage.



          If in fact you actually always iterate "all" of the results (and that includes a "limit" which is a "cursor modifier") then the cursor will close and all is okay.



          General usage will be that you actually exhaust/deplete the cursor by going through all of the results. Therefore there is no explicit need to destroy.






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49













          14












          14








          14







          Closing the cursor is only really required when you do not "exhaust" the results. Or in other terms, iterate over all the possible results returned by the cursor.



          Leaving a "cursor" open is like leaving an open connection that never gets re-used. These things are not free. In fact the standard connection cost is 1MB (approx). So if you are leaving a lot of "partially iterated" cursors hanging around there is a general overhead in terms of an active connection and it's memory usage.



          If in fact you actually always iterate "all" of the results (and that includes a "limit" which is a "cursor modifier") then the cursor will close and all is okay.



          General usage will be that you actually exhaust/deplete the cursor by going through all of the results. Therefore there is no explicit need to destroy.






          share|improve this answer













          Closing the cursor is only really required when you do not "exhaust" the results. Or in other terms, iterate over all the possible results returned by the cursor.



          Leaving a "cursor" open is like leaving an open connection that never gets re-used. These things are not free. In fact the standard connection cost is 1MB (approx). So if you are leaving a lot of "partially iterated" cursors hanging around there is a general overhead in terms of an active connection and it's memory usage.



          If in fact you actually always iterate "all" of the results (and that includes a "limit" which is a "cursor modifier") then the cursor will close and all is okay.



          General usage will be that you actually exhaust/deplete the cursor by going through all of the results. Therefore there is no explicit need to destroy.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 17 '14 at 10:21









          Neil LunnNeil Lunn

          102k23181189




          102k23181189












          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49

















          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49
















          Thank you for the answer.

          – Pratik Patel
          Jun 17 '14 at 10:49





          Thank you for the answer.

          – Pratik Patel
          Jun 17 '14 at 10:49













          3














          It depends on your usage, but at least in my web application - the client handles the closing.
          Why? because my web application follows the pattern of short & stateless request handling (you get a request from the browser, build an HTTP response quickly - less than a second - and this response relies on mongo data). So my client only needs the connection for 1 second.



          Now, consider what if I have (say) 50 requests per minute... my server handles them comfortably. But it would have crashed if each request were to hold on to resources for 10 minutes... E.g. after 9 minutes I would have 450 unnecessary open resources...






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49















          3














          It depends on your usage, but at least in my web application - the client handles the closing.
          Why? because my web application follows the pattern of short & stateless request handling (you get a request from the browser, build an HTTP response quickly - less than a second - and this response relies on mongo data). So my client only needs the connection for 1 second.



          Now, consider what if I have (say) 50 requests per minute... my server handles them comfortably. But it would have crashed if each request were to hold on to resources for 10 minutes... E.g. after 9 minutes I would have 450 unnecessary open resources...






          share|improve this answer























          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49













          3












          3








          3







          It depends on your usage, but at least in my web application - the client handles the closing.
          Why? because my web application follows the pattern of short & stateless request handling (you get a request from the browser, build an HTTP response quickly - less than a second - and this response relies on mongo data). So my client only needs the connection for 1 second.



          Now, consider what if I have (say) 50 requests per minute... my server handles them comfortably. But it would have crashed if each request were to hold on to resources for 10 minutes... E.g. after 9 minutes I would have 450 unnecessary open resources...






          share|improve this answer













          It depends on your usage, but at least in my web application - the client handles the closing.
          Why? because my web application follows the pattern of short & stateless request handling (you get a request from the browser, build an HTTP response quickly - less than a second - and this response relies on mongo data). So my client only needs the connection for 1 second.



          Now, consider what if I have (say) 50 requests per minute... my server handles them comfortably. But it would have crashed if each request were to hold on to resources for 10 minutes... E.g. after 9 minutes I would have 450 unnecessary open resources...







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 17 '14 at 10:19









          Pelit MamaniPelit Mamani

          1,6012911




          1,6012911












          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49

















          • Thank you for the answer.

            – Pratik Patel
            Jun 17 '14 at 10:49
















          Thank you for the answer.

          – Pratik Patel
          Jun 17 '14 at 10:49





          Thank you for the answer.

          – Pratik Patel
          Jun 17 '14 at 10:49

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f24260677%2fmongodb-why-should-we-close-the-cursor-after-it-is-used%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          1928 у кіно

          Захаров Федір Захарович

          Ель Греко