2016-03-21 47 views
0

我得到這個錯誤重複:希望通過MongoDB中的一半進行迭代,並通過半的休息與另一個查詢

Exception in thread "main" com.mongodb.MongoCursorNotFoundException: Query failed with error code -5 and error message 'Cursor 304054517192 not found on server mongodb2:27017' on server mongodb2:27017 at com.mongodb.operation.QueryHelper.translateCommandException(QueryHelper.java:27) at com.mongodb.operation.QueryBatchCursor.getMore(QueryBatchCursor.java:215) at com.mongodb.operation.QueryBatchCursor.hasNext(QueryBatchCursor.java:103) at com.mongodb.MongoBatchCursorAdapter.hasNext(MongoBatchCursorAdapter.java:46) at com.mongodb.DBCursor.hasNext(DBCursor.java:155) at org.jongo.MongoCursor.hasNext(MongoCursor.java:38) at com.abc.Generator.Generate(Generator.java:162) at com.abc.main.main(main.java:72)

我以爲是因爲查詢運行時間過長。
所以我打算使用find()查詢mongo並遍歷一半的集合。
然後,我想使用另一個find()查詢並遍歷剩餘的一半集合。

你可以幫助你如何直接將光標置於集合的第一位嗎? The documentation does not seem to provide它的任何功能。

我基本上只是使用find()並迭代通過集合100000記錄,而通過ssh連接到服務器。

MongoCollection history = jongo.getCollection("historyCollection"); 
MongoCursor<MyClass> allHistories = history.find().as(MyClass.class); 

    //---Iterate thru all histories 
    while (allHistories.hasNext()) { 
    MyClass oneHistory = allHistories.next(); 
} 

回答

0

通過讓ObjectId定購的時間戳爲Mongo集合解決了它。這樣,我就可以使用大於運算符來查找objectID並拆分迭代。

private MongoCursor<PersonDBO> ReadFewProfilesFromDB(final String objectIdAfterWhichToSearchFrom, final Integer FIND_LIMIT) { 

    MongoCursor<PersonDBO> aBatchOfProfiles = null; 

    try { 
     if (objectIdAfterWhichToSearchFrom.equals(START_OBJECTID_OF_MONGO_BATCHES)) { 
      aBatchOfProfiles = personProfile.find().limit(FIND_LIMIT).as(PersonDBO.class); 
     } else { 
      aBatchOfProfiles = personProfile.find("{_id: {$gt: #}}", new ObjectId(objectIdAfterWhichToSearchFrom)).limit(FIND_LIMIT).as(PersonDBO.class); 
     } 
    } catch(Exception e) {logger.error("Problem while trying to find {} personProfiles, starting from objectID {}. {}, {}", FIND_LIMIT, objectIdAfterWhichToSearchFrom, e.getMessage(), e.getCause());} 

    if (aBatchOfProfiles == null) { 
     logger.error("profiles collection is null. Nothing more to iterate OR there was an exception when finding profiles. If exception, there would be an error printed above."); 
     return null; 
    }   

    return aBatchOfProfiles; 
}