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 7166803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:28:20+00:00 2026-05-28T14:28:20+00:00

I am writing a Maintenance Script for MongoDB that will compact collections from a

  • 0

I am writing a Maintenance Script for MongoDB that will compact collections from a replica set on a scheduled basis. The script I have so far looks good, doing the job it is expected to do against Primary and Secondary nodes. There is a problem, however:

The system that is using the MongoDB Replica Set is a high-availability web-queue, which is constantly being written to and read from. Thus even the several seconds downtime when calling rs.StepDown() are absolutely unacceptable. Is there a way to safely demote a Primary node without MongoExceptions from the hundreds of clients?

Thanks!

p.s. here is the actual version of the script that should be run via a cron-job at low-load times once a month

// assuming we are a replica set ;-)
if(rs.isMaster().setName){
    try {
        //check if the script is run against a master
        if(rs.isMaster().ismaster){ //if so, step down as master        
            print("Connected to a PRIMARY node")
            print("Will try to step down as primary");
            rs.stepDown();

            // after stepdown connections are dropped. do an operation to cause reconnect:
            rs.isMaster();            

            // now ready to go.      
            // wait for another node to become primary -- it may need data from us for the last 
            // small sliver of time, and if we are already compacting it cannot get it while the 
            // compaction is running.            
            var counter = 1;
            while( 1 ) { 
                var m = rs.isMaster();
                if( m.ismaster ) {
                    print("ERROR: no one took over during our stepDown duration. we are primary again!");
                    assert(false);
                }

                if( m.primary ){ // someone else is, great
                    print("new master host is: "+m.primary);
                    break; 
                }
                print("waiting "+counter+" seconds");
                sleep(1000);
                counter++;
            }
        }else{
            print("Connected to a SECONDARY node");
            print("Going into Recovery Mode and Compacting");            
        }

        // someone else is primary, so we are ready to proceed with a compaction
        print(" ");
        print("Compacting...");
        print("- queue");
        printjson( db.runCommand({compact:"queue"}) );
        print(" ");

    } catch(e) { 
        print(" ");
        print("ACHTUNG! Exception:" + e);
        print(" ");
    }
}else{
    print('This script works with replica sets only');
}
  • 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-05-28T14:28:21+00:00Added an answer on May 28, 2026 at 2:28 pm

    Is there a way to safely demote a Primary node without MongoExceptions from the hundreds of clients?

    No. MongoDB does not have any form of “anticipatory change-over” or “maintenance change-over”.

    Doing this cycle of compactions is the correct thing to do. But you will have to wait for a maintenance window to compact the primary.

    … is a high-availability web-queue.

    Are you using Capped Collections for this? Capped collections don’t need compacting. The objects in a capped collection cannot be resized, but that is typically not a problem for Queue objects.

    Though not an ideal solution it may solve your problem.

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

Sidebar

Related Questions

I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005)
Okay so I'm writing a SQL Server 2008 Stored Procedure (maintenance script). In doing
I'm writing some PHP that will interact with MySQL to 1. upload and 2.
I am writing a scheduled job to mimic a SQL Server maintenance plan for
I'm writing a webapp that will only be used by authenticated users. Some temporary
I am writing a maintenance plan where databases will be detached then the content
I'm writing a script to perform some database maintenance, and MySQL is not accepting
Background: writing an automated release script to export changed files between versions from SVN
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the
Writing a test app to emulate PIO lines, I have a very simple Python/Tk

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.