想要遍历 mongoDB 的一半并使用另一个查询遍历其余的一半
Want to iterate through half of mongoDB and iterate through the rest of the half with another query
我收到此错误:
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();
}
通过让 Mongo 集合按 ObjectId 的时间戳排序解决了这个问题。这样,我就能够使用大于运算符来查找 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;
}
我收到此错误:
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();
}
通过让 Mongo 集合按 ObjectId 的时间戳排序解决了这个问题。这样,我就能够使用大于运算符来查找 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;
}