Article From:https://segmentfault.com/q/1010000011076738
Question:

When you go through mongoDB, the following third lines of code (cursor.hasNext ()) are wrong.
Related code:

MongoCursor<Document> cursor = collection.find().batchSize(10000).iterator();
try {
    while (cursor.hasNext()) {
        Document document = cursor.next();
        String url = document.getString("Collect URL ");}} catch (Exception E) {E.printStackTrace ();} finally {Cursor.close ();}

Java1.8 monogDB v3.2.7

Answer 0:

thiscursor Return isiterator() , Probably nothasNext() Method

Answer 1:

com.mongodb.MongoCursorNotFoundException: Query failed with error code
-5 and error message ‘Cursor 18253096929 not found on server localhost:27017’ on server localhost:27017 at
com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:90)
at
com.mongodb.connection.GetMoreProtocol.execute(GetMoreProtocol.java:49)
at
com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at
com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:260)
at
com.mongodb.connection.DefaultServerConnection.getMore(DefaultServerConnection.java:225)
at
com.mongodb.operation.QueryBatchCursor.getMore(QueryBatchCursor.java:195)
at
com.mongodb.operation.QueryBatchCursor.hasNext(QueryBatchCursor.java:91)
at
com.mongodb.MongoBatchCursorAdapter.hasNext(MongoBatchCursorAdapter.java:46)
at com.downloader.DownloadHtml.saveHtml(DownloadHtml.java:75) at
com.downloader.DownloadHtml.main(DownloadHtml.java:23)

From the unusual information you give, this error is not reported at the beginning, but is being done.GetMoreAt the time. That is to say, the cursor was normally executed at the beginning and later failed. A few possible reasons I can think of:

The idle time of the cursor is too long

Refer to the document on the cursor,

By default, the server will automatically close the cursor after 10 minutes of inactivity, or if client has exhausted the cursor. To override this behavior in the mongo shell, you can use the cursor.noCursorTimeout() method

Although it is shell, it is the same reason in the Java driver, and the corresponding parameters are looked for by themselves.

Load balancing of your own configuration

Some users add in front of mongodbkeepalivedOr other components with similar functions, causing the request to be forwarded to the wrong node so that the previous cursor could not be found. Mongodb with the driver itself can achieve high availability, and there is no need to add any load balancing in front.

See if you are one of the two.

Link of this Article: MongoCursorNotFoundException

Leave a Reply

Your email address will not be published. Required fields are marked *