MongoDB.live, free & fully virtual. June 9th - 10th. Register Now MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Seeding MongoDB nodejs

Hello everybody.

Seeking for advice about a nodejs app and mongoDB seeding.

I have a nodejs app which it should be able to seed a mongodb once i export a variable DB_HOST which the correct IP.

I am able to go through all the steps and be able to ping my mongodb instance from my app instance.

When i run the command node seeds/seed.js

i get:

ubuntu@ip-10-0-104-49:~/AppFolder/app$ node seeds/seed.js 
(node:1799) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
Database Cleared
Database Seeded

but if i log in my mongodb instance and in mongo console
run show dbs;

all i have is:

rs0:PRIMARY> show dbs;
local  0.000GB
posts  0.000GB

the posts, should have some data in, as my node seeds/seed.js should create some random posts and pass them to mongodb posts.

now, i checked my mongodb logs for troubleshooting but i dont have any error, actually it says that he recived clientdata from my app instance:

2020-05-13T01:47:32.158+0000 I REPL     [ReplicationExecutor] New replica set config in use: { _id: "rs0", version: 3, protocolVersion: 1, members: [ { _id: 0, host: "ip-10-0-1-100:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 1, host: "ip-10-0-2-100:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 }, { _id: 2, host: "ip-10-0-3-100:27017", arbiterOnly: false, buildIndexes: true, hidden: false, priority: 1.0, tags: {}, slaveDelay: 0, votes: 1 } ], settings: { chainingAllowed: true, heartbeatIntervalMillis: 2000, heartbeatTimeoutSecs: 10, electionTimeoutMillis: 10000, getLastErrorModes: {}, getLastErrorDefaults: { w: 1, wtimeout: 0 }, replicaSetId: ObjectId('5ebb51b31fa00dc07eacc09f') } }
2020-05-13T01:47:32.158+0000 I REPL     [ReplicationExecutor] This node is ip-10-0-1-100:27017 in the config
2020-05-13T01:47:32.158+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Connecting to ip-10-0-3-100:27017
2020-05-13T01:47:32.161+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to ip-10-0-3-100:27017, took 4ms (2 connections now open to ip-10-0-3-100:27017)
2020-05-13T01:47:32.161+0000 I REPL     [ReplicationExecutor] Member ip-10-0-2-100:27017 is now in state STARTUP2
2020-05-13T01:47:32.162+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Successfully connected to ip-10-0-3-100:27017, took 4ms (2 connections now open to ip-10-0-3-100:27017)
2020-05-13T01:47:32.163+0000 I REPL     [ReplicationExecutor] Member ip-10-0-3-100:27017 is now in state STARTUP
2020-05-13T01:47:33.026+0000 I REPL     [rsSync] transition to primary complete; database writes are now permitted
2020-05-13T01:47:34.162+0000 I REPL     [ReplicationExecutor] Member ip-10-0-2-100:27017 is now in state SECONDARY
2020-05-13T01:47:34.164+0000 I REPL     [ReplicationExecutor] Member ip-10-0-3-100:27017 is now in state SECONDARY
2020-05-13T01:47:59.204+0000 I NETWORK  [conn54] received client metadata from 10.0.104.49:57540 conn54: { driver: { name: "nodejs", version: "2.2.34" }, os: { type: "Linux", name: "linux", architecture: "x64", version: "4.4.0-1106-aws" }, platform: "Node.js v8.17.0, LE, mongodb-core: 2.1.18" }
2020-05-13T01:48:32.162+0000 I ASIO     [NetworkInterfaceASIO-Replication-0] Ending idle connection to host ip-10-0-3-100:27017 because the pool meets constraints; 1 connections to that host remain open

but still my posts is empty.

Any idea about why is this happening? i always had a problem with step.

i dont know if this might help but i post the important parts of the app js:

this is posts.js

var mongoose = require('mongoose');

var PostSchema = new mongoose.Schema({
  title: String,
  body: String
});


module.exports = mongoose.model('Post', PostSchema);

and this is the seeds.js:

var Post = require('../models/post');
var mongoose = require('mongoose');
var faker = require('faker');

if(process.env.DB_HOST) {
  mongoose.connect(process.env.DB_HOST);

  Post.remove({} , function(){
    console.log('Database Cleared');
  });

  var count = 0;
  var num_records = 100;

  for(var i = 0; i < num_records; i++) {
    Post.create({
      title: faker.random.words(),
      body: faker.lorem.paragraphs()
    }, function(){
      count++;
      if(count >= num_records) {
        mongoose.connection.close();
        console.log("Database Seeded");
      }
    }); 
  }
}

all what i had to do, is set the DB_HOST to a mongodb link.

which in my case is:
export DB_HOST=mongodb://ip-10-0-1-100:27017/posts,ip-10-0-2-100:27017/posts,ip-10-0-3-100:27017/posts?replicaSet=rs0

there are 3 because i am working with a replicaSet. i have the IPs set in etc/hosts and i am able to ping all of them from my app instance.

Thank you in advance.