[英]How to call db.Collection.stats() from Mongo java driver
[英]Mongo DB Java Driver Cursor Doesn't contain full Collection
我目前有一个游标正在遍历MongoDB集合,并且正在取出几个不同的值并将它们添加到另一个表中。 但是我注意到,当进程运行时,光标不会覆盖集合中的所有文档(通过添加计数器可以发现)。
Beacon Lookup Collection有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
参考进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.