简体   繁体   中英

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. 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.

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.

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. Update that with doc reference too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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