Init script for replicaSet in Mongo container

59 Views Asked by At

I'm trying to test the ACID transaction for my replicaSet mongoDB. Is there anyway to initialize my mongoDB script in .js that look something like this

db.getCollection("myCollection").insert(
 ....
)

I've tried to initialize a mongo container for testing, in here I copied my init .js file into /docker-entrypoint-initdb.d the default folder to initialize script

GenericContainer mongodb = new GenericContainer("mongo:5")
                .withNetworkAliases("mongodbalias")
                .withCommand("--replSet myReplicaSet")
                .withEnv("MONGO_INITDB_DATABASE", "myDatabase")
                .withExposedPorts(27017)
                .withCopyFileToContainer(MountableFile.forClasspathResource("resources/db/initScript"), "/docker-entrypoint-initdb.d");

However this came to an error:

    "errmsg" : "not master",
    "code" : 10107,
    "codeName" : "NotWritablePrimary"

this is due to my mongoDB is initialized with replicaSet

0

There are 0 best solutions below