I have some problems with MongoDB.
I have 2 replicas of NodeJS and 1 MongoDB. Default data is loaded always twice in the database. How to fix this?
I have databaseLoader.js
function which is load data in DB:
mongoose.promise = Promise;
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.connect(MONGODB_URI, {useNewUrlParser: true})
.then(
() => {
logger.info('Successfully connected to mongoDB');
loader.loadDefaultData()
.then(response => {
});
},
)
.catch(err => {
logger.error('Connection to MongoDB could not be established');
});
I don't know for what purpose you run 2 replicas to load demo data and connect to db but if you have 2 replicas in your deployment, then each replica will run independently, so it means it will load demo data twice.
If you have some application and you want to check if there is db connectivity, before starting the application, you can use initContainer
Init Containers are exactly like regular Containers, except:
They always run to completion.
Each one must complete successfully before the next one is started.