Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8443925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:16:13+00:00 2026-06-10T09:16:13+00:00

I have a groovy script which uses the Mongo Java Driver mongo-java-driver-2.8.0.jar to visit

  • 0

I have a groovy script which uses the Mongo Java Driver mongo-java-driver-2.8.0.jar to visit all there records in a single collection updating any that do not match the expected structure. The script works like a champ, but I am left scratching my head why it processes more records than the collection actually has. Or, more accurately, the dbCursore.hasNext() iterates over more records than the collection actually has. This only occurs if the script finds something to update. If the script executes with no updates, the total number processed is correct.

Does hasNext() “start over” or do records move within the iteration if they’ve been updated?

Here’s the code…

static def doIt( mongo, normalizer, isDryRun ) {
    def ttlProcessed = 0
    def ttlCandidates = 0
    def ttlUpdated = 0
    def lapCount = 0;
    def lapStartTime = System.currentTimeMillis();

    def db = mongo.getDB( "devices" )
    DBCollection dbCollection = db.getCollection( "profiles" )
    DBCursor dbCursor = dbCollection.find();
    while ( dbCursor.hasNext() ) {
        DBObject source = dbCursor.next();
        DBObject normalized = normalizer.normalize( source )
        // Only update if changed...
        if ( ! ( source.equals( normalized ) ) ) {
            ttlCandidates++
            if ( !isDryRun ) {
                BasicDBObject searchQuery = new BasicDBObject( "_id", normalized.get( "_id" ) )
                WriteResult result = dbCollection.update( searchQuery, normalized, false, false, WriteConcern.SAFE );
                ttlUpdated++
            }
        }
        ttlProcessed++;
        if ( ttlProcessed % 10000 == 0 ) {
            printErr "split: ${lapCount}, splitElapsed: ${calcElapsed( lapStartTime) } ms, elapsed: ${calcElapsed( startTime )} ms, processed: ${ttlProcessed}, candidates: ${ttlCandidates}, updated: ${ttlUpdated}"
            lapCount++
            lapStartTime = System.currentTimeMillis()
        }
    }
    printErr "split: ${lapCount}, splitElapsed: ${calcElapsed( lapStartTime) } ms, elapsed: ${calcElapsed( startTime )} ms, processed: ${ttlProcessed}, candidates: ${ttlCandidates}, updated: ${ttlUpdated}"
}

How is it that ttlProcessed gets a value higher than the count of the collection being processed if the run updates any records?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:16:14+00:00Added an answer on June 10, 2026 at 9:16 am

    This can happen because an update causes a document to move (usually because of growth). If a document does grow and is processed again, it will count as an iteration, but assuming your update is idempotent (I have not tested), then it will not be an issue.

    You can use the $snapshot option to get around this if it is a concern. I would also recommend reading this:

    http://www.mongodb.org/display/DOCS/How+to+do+Snapshotted+Queries+in+the+Mongo+Database

    These issues are essentially why tools like mongodump and mongoexport walk the _id index (i.e. use $snapshot by default).

    If you want to look into preventing the moves in the first place, then take a look at this section on the Padding Factor page:

    http://www.mongodb.org/display/DOCS/Padding+Factor#PaddingFactor-ManualPadding

    There are also options in 2.2 to set padding when running the compact command:

    http://docs.mongodb.org/manual/release-notes/2.2/#padding-specifiable-on-compact-command

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project which uses groovy 1.7.5 and grails 1.3.4. but my new
I have one groovy script which print some statistics: println: ... now I have
I have a fairly complicated groovy script which performs some operations on an Oracle
I have a groovy script that needs a library in a jar. How do
In my java web application, I have a single background-worker thread, which requires a
Similar to this question I have a small groovy test script that basically uses
I'm learning ruby (have a java/groovy background) and it looks like when writing methods,
I have following script which is aim to make a war file. def ant
We have an existing java-based heavyweight project that needed an interactive script interpreter. After
I’m working on a Java server app which uses Spring 3 and C3P0 to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.