Nafaz M N M
Nafaz M N M

Reputation: 1698

Insert Data Into MongoDB Container at the Spring Boot Application initialization

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

Upvotes: 0

Views: 1593

Answers (2)

Nafaz M N M
Nafaz M N M

Reputation: 1698

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

init_scripts/mongo_init.js

var db = connect("mongodb://admin:[email protected]: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' 

Upvotes: 1

Schwarz54
Schwarz54

Reputation: 1004

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.

Upvotes: 1

Related Questions