Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-42274

Using forEach to iterate a cursor results in error "hasNext: false" in v1.3.0

    • Type: Icon: Bug Bug
    • Resolution: Incomplete
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Querying
    • Labels:
      None
    • Server Triage
    • ALL
    • Hide

      db = db.getMongo().getDB("usa");

      cursor = db.cities.find({_id: '17662'}).limit(100);

      cursor_explain = cursor.explain();
      cursor_count = cursor.count();
      cursor_size = cursor.size();

      db.getSisterDB("results")[TEST_NAME + ".explain"].insert({explain: tojson(cursor_explain)});

      cursor.forEach(function(doc) {db.getSisterDB("results")[TEST_NAME].insert(doc);});

      Show
      db = db.getMongo().getDB("usa"); cursor = db.cities.find({_id: '17662'}).limit(100); cursor_explain = cursor.explain(); cursor_count = cursor.count(); cursor_size = cursor.size(); db.getSisterDB("results") [TEST_NAME + ".explain"] .insert({explain: tojson(cursor_explain)}); cursor.forEach(function(doc) {db.getSisterDB("results") [TEST_NAME] .insert(doc);});

      While performing an academic experiment based on the random generation of a large set of tests to exercise the Query Language of MongoDB (all of which syntactically valid), a possible bug was detected in several tests while iterating a Cursor object to store query results to a new collection.

      It is unknown to us why it occurred, but because the method forEach() is the one responsible for iterating a Cursor object through the while( hasNext() ) { next() } loop, it is unlikely that the actual code in the respective test cases could have caused such an error.

      Additionally, because no similar bug has been reported, we decided to do so for consistency-sake, even though it was found on very very old and out-of-date version of MongoDB and may now hold little importance.

      Attached is a zip file with all the 14 test cases in which this event occurred. For clarity, note that each test case (i.e., file) contains a set of 4 "sub-"test cases, which respectively test the find(), find().hint(), find.sort() and find.hint().sort() method combinations. We suspect that the error is thrown by the forEach() method, which is used once per each of the 4 blocks of code.

      The tests were carried out on the USA cities database.

            Assignee:
            backlog-server-triage [HELP ONLY] Backlog - Triage Team
            Reporter:
            jagnelo@student.dei.uc.pt João Agnelo
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: