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
3
  • Does this answer your question? How can you load initial data in MongoDB through Spring Boot? Commented May 27, 2021 at 4:47
  • I am asking MongoDB container. in that question about local MongoDB which is hosted in the local machine Commented May 27, 2021 at 4: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. Commented May 27, 2021 at 9:40

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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' 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.