1

The MongoDB docs state that one can use the mongo client console to initialize replica sets by running rs.initiate([config]). However, I don't want to have to manually use the command line for this. There is a driver method called Admin.command in the docs but I don't know how that would work here. How do I automate configuring replica sets using the MongoDB JS driver?

I have a js script that initializes my database installation by creating the data folder and automatically setting up users for access control. I want it to also configure the replica set so the database is completely ready to use after running it. I'm using MongoDB 4.2.1 and driver version 3.3.

1

The method rs.initiate can only be used in the MongoDB shell, for drivers you have to use Database Commands.

For example:

myMongoClientVariable.db("myDB").admin().command( { 
    replSetInitiate : {
        _id : "myReplicaSet",
        members : [
            {_id : 0, host : "host0"},
            {_id : 1, host : "host1"},
            {_id : 2, host : "host2"}
        ]
    }
} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.