我收到此错误:
线程"main"中的异常 com.mongodb.MongoCursorNotFoundException: 查询失败,错误代码为 -5,错误消息"游标304054517192 在服务器上找不到 Mongob2:27017' 在服务器 Mongob2:27017 at com.mongodb.operation.QueryHelper.translateCommandException(QueryHelper.java:27) 在 com.mongodb.operation.QueryBatchCursor.getMore(QueryBatchCursor.java:215) 在 com.mongodb.operation.QueryBatchCursor.hasNext(QueryBatchCursor.java:103) 在 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()
查询并循环访问集合的剩余一半。
你能帮忙把光标直接放在集合的一半位置吗?该文档似乎没有为其提供任何功能。
我基本上只是使用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();
}
通过让 Mongo 集合按时间戳的 ObjectId 排序来解决它。这样,我就可以使用大于运算符来查找对象 ID 并拆分迭代。
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;
}