<p>I have some problems with MongoDB.</p>

<p>I have 2 replicas of NodeJS and 1 MongoDB. Default data is loaded always twice in the database. How to fix this?</p>

<p>I have <code>databaseLoader.js</code> function which is load data in DB:</p>

<pre><code>mongoose.promise = Promise;
mongoose.set('useCreateIndex', true);
mongoose.set('useFindAndModify', false);
mongoose.connect(MONGODB_URI, {useNewUrlParser: true})
    .then(
        () =&gt; {
            logger.info('Successfully connected to mongoDB');
            loader.loadDefaultData()
                .then(response =&gt; {
                });
        },
    )
    .catch(err =&gt; {
        logger.error('Connection to MongoDB could not be established');
    });
</code></pre>

Mirnes Halilović
<p>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.</p>
<p>If you have some application and you want to check if there is db connectivity, before starting the application, you can use <a href="https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#understanding-init-containers" rel="nofollow noreferrer">initContainer</a></p>
<blockquote>
<p>Init Containers are exactly like regular Containers, except:</p>
<ul>
<li><p>They always run to completion.</p>
</li>
<li><p>Each one must complete successfully
before the next one is started.</p>
</li>
</ul>
</blockquote>
