我目前有一个游标正在浏览MongoDB集合,并取出几个不同的值并将它们添加到另一个表中。但是,我注意到当进程运行时,光标并没有覆盖集合中的所有文档(通过添加计数器发现)。
信标查找集合有 3342 个文档,但从日志记录中我只能看到它迭代了其中的 1114 个文档并完成游标而没有错误。调试时查看光标,它确实包含所有 3343 个文档。
以下是我尝试运行但当前遇到问题的方法:
public void flattenCollection(){
MongoCollection<Document> beaconLookup = getCollection("BEACON_LOOKUP");
MongoCollection<Document> triggers = getCollection("DIM_TRIGGER");
System.out.println(beaconLookup.count());
// count = 3342
long count = beaconLookup.count();
MongoCursor<Document> beaconLookupCursor = beaconLookup.find().batchSize((int) count).noCursorTimeout(true).iterator();
MongoCursor<Document> triggersCursor = triggers.find().iterator();
try {
while (beaconLookupCursor.hasNext()) {
int major = (Integer) beaconLookupCursor.next().get("MAJOR");
int minor = (Integer) beaconLookupCursor.next().get("MINOR");
if(major==1215) {
System.out.println("MAJOR " + major + " MINOR " + minor);
}
triggers.updateMany(and(eq("MAJOR", major),
eq("MINOR", minor)),
combine(set("BEACON_UUID",beaconLookupCursor.next().get("UUID"))));
count = count - 1;
System.out.println(count);
}
} finally {
beaconLookupCursor.close();
}
}
任何建议都会很棒!
每次
迭代都会多次调用next()
。
改变
int major = (Integer) beaconLookupCursor.next().get("MAJOR");
int minor = (Integer) beaconLookupCursor.next().get("MINOR");
自
Document doc = beaconLookupCursor.next();
int major = (Integer) doc.get("MAJOR");
int minor = (Integer) doc.get("MINOR");
看起来还有一个对 UUID 的调用。也用doc
参考更新它。