简体   繁体   English

Mongo DB Java驱动程序游标不包含完整的Collection

[英]Mongo DB Java Driver Cursor Doesn't contain full Collection

I currently have a cursor that is going through a MongoDB Collection and is taking out a couple of different values out and adding them to another table. 我目前有一个游标正在遍历MongoDB集合,并且正在取出几个不同的值并将它们添加到另一个表中。 However I have noticed that when the process is running the cursor isn't covering all the documents within the collection (Found out by adding counter). 但是我注意到,当进程运行时,光标不会覆盖集合中的所有文档(通过添加计数器可以发现)。

The Beacon Lookup Collection has 3342 documents, but from the logging I can only see it's iterated through 1114 of them and finishes the cursor with no error.Looking at the cursor when debugging it does contain all 3343 documents. Beacon Lookup Collection有3342个文档,但是从日志记录中我只能看到它遍历了1114个文档,并且没有错误地完成了游标。调试时查看游标确实包含了所有3343个文档。

Below is the method I am trying to run and currently having issues with: 以下是我尝试运行的方法,当前存在问题:

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();
        }
    }

Any advice would be great! 任何建议都很好!

You are calling next() more than one time for each iteration. 每次迭代都多次调用next()

Change 更改

int major = (Integer) beaconLookupCursor.next().get("MAJOR");
int minor = (Integer) beaconLookupCursor.next().get("MINOR");

to

Document doc = beaconLookupCursor.next();
int major = (Integer) doc.get("MAJOR");
int minor = (Integer) doc.get("MINOR");

Looks like there is one more call for UUID. 似乎还有一个UUID呼叫。 Update that with doc reference too. 也使用doc参考进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM