![](/img/trans.png)
[英]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.