0

I have to insert some sample data into Collection in MongoDB when the application starts to run. I have done the following steps but it didn't work.

  1. created the entrypoint.js under the init_script folder

entrypoint.js

 use admin;

db.createUser(
    {
        user: "patient_db",
        pwd: "14292",
        roles: [ { role: "readWrite", db: "patient_db" } ]
    }
);

db.grantRolesToUser( "patient_db", [{ role: "readWrite", db: "patient_db"}]); 
  1. created data.js file in the resources path

src/main/resources/data.js

use patient_db;

db.createCollection("holiday");

db.holiday.insert({holiday_date:'25-12-2021',holiday_name:'Christmas',created_by:'John Wick',modified_by:'John_Wick',created_date_time:'2021-04-25 04:23:55',modified_date_time:'2021-04-25 04:23:55'});
  1. configured the docker-compose.yml

docker-compose.yml

 version: "3"

services:
  patient-service:
    image: patient-service:1.0
    container_name: patient-service
    ports:
    - 9090:9090
    restart: on-failure
    networks:
      - patient-mongo
    depends_on:
      - mongo-db
    links:
      - mysql-db
  mongo-db:
    image: mongo:latest
    container_name: mongo-db
    ports:
      - 27017:27017
    networks:
      - patient-mongo
    volumes:
     - 'mongodata:/data/db'
     - './init_scripts:/docker-entrypoint-initdb.d' 
    environment:
     - MONGO_INITDB_ROOT_USERNAME=admin
     - MONGO_INITDB_ROOT_PASSWORD=14292
    restart: unless-stopped
    

networks:
  patient-mongo:
  
volumes:
  mongodata:

4.Finally, Connection with MongoDB

properties-dev.yml

spring:
    data:
      mongodb:
        host: mongo-db
        port: 27017
        database: patient_db
Nafaz M N M
  • 1,558
  • 2
  • 27
  • 41
  • Does this answer your question? [How can you load initial data in MongoDB through Spring Boot?](https://stackoverflow.com/questions/47678465/how-can-you-load-initial-data-in-mongodb-through-spring-boot) – Randy Casburn May 27 '21 at 04:47
  • I am asking MongoDB container. in that question about local MongoDB which is hosted in the local machine – Nafaz M N M May 27 '21 at 04:52
  • There shouldn't be anything different if the database is running in a container, or on the same system, or in the cloud; while the question @RandyCasburn mentions doesn't have a lot of specifics I'd expect the overall techniques there to work in a Docker-based setup too. – David Maze May 27 '21 at 09:40

2 Answers2

1

This is how I insert the entrypoint code to mongodb container:

  1. Create a .sh file (example.sh)
  2. Create mongo users and the data you want to insert.

example.sh

#!/usr/bin/env bash
echo "Creating mongo users..."
mongo admin --host localhost -u root -p mypass --eval "

db = db.getSiblingDB('patient_db');
db.createUser(
 {
    user: "patient_db",
    pwd: "14292",
    roles: [ { role: "readWrite", db: "patient_db" } ]
 }
);
 db.createCollection('holiday');

db.holiday.insert({holiday_date:'25-12-2021',holiday_name:'Christmas',
created_by:'John Wick',modified_by:'John_Wick',created_date_time:'2021-04-25 04:23:55',modified_date_time:'2021-04-25 04:23:55'});

"

echo "Mongo users and data created."
  1. At docker-compose, insert the entrypoint

    volumes:
    - 'mongodata:/data/db'
    - './example.sh:/docker-entrypoint-initdb.d/example.sh'
    

Maybe its not the more clean option, but its works perfectly.

I did it like this because I didn't get it to work with js files.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Schwarz54
  • 964
  • 1
  • 9
  • 18
1

Thanks, @Schwarz54 for your answer. It works with js file

init_scripts/mongo_init.js

var db = connect("mongodb://admin:14292@127.0.0.1:27017/admin");

db = db.getSiblingDB('patient_db'); /* 'use' statement doesn't support here to switch db */

db.createUser(
    {
        user: "patient_db",
        pwd: "14292",
        roles: [ { role: "readWrite", db: "patient_db" } ]
    }
);


db.createCollection("holiday");

db.holiday.insert({holiday_date:'25-12-2021',holiday_name:'Christmas',created_by:'John Wick',modified_by:'John_Wick',created_date_time:'2021-04-25 04:23:55',modified_date_time:'2021-04-25 04:23:55'});

docker-compose.yml

volumes:
     - 'mongodata:/data/db'
     - './init_scripts:/docker-entrypoint-initdb.d' 
Nafaz M N M
  • 1,558
  • 2
  • 27
  • 41