Home > OS >  MongoDB on Docker - does not import CSV data
MongoDB on Docker - does not import CSV data

Time:07-23

I managed to run MongoDB and Mongo-Express via docker-compose.yaml on IntelliJ. I want to use the scripts to create DB, collections and load test data from CSV file at startup.

When starting up the mongoDB, database and collection on it from file 01-create-db.js is created, it means that this script is working. But I am not able to see the data that has to be imported - script is in the second file 02-init.sh. What is wrong?

Here is the listing from files: docker-compose.yaml:

version: "3.8"
services:
  mongodb:
    image: mongo
    container_name: mongodb
    ports:
      - 27017:27017
    volumes:
      - ./init-mongodb:/docker-entrypoint-initdb.d
      - ./init-mongodb/data:/tmp/data
    environment:
      - MONGO_INITDB_ROOT_USERNAME=rootuser
      - MONGO_INITDB_ROOT_PASSWORD=rootpass
  mongo-express:
    image: mongo-express
    container_name: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      - ME_CONFIG_MONGODB_ADMINUSERNAME=rootuser
      - ME_CONFIG_MONGODB_ADMINPASSWORD=rootpass
      - ME_CONFIG_MONGODB_SERVER=mongodb
    depends_on:
      - mongodb
volumes:
  data: {}
networks:
  default:
    name: mongodb_network

01-create-db.js

// creating database and collections:
db = new Mongo().getDB("testdb1");
db.createCollection("transactions", {capped: false});

02-init.sh

#!/bin/bash

echo "########### Loading data to Mongo DB ###########"

mongoimport --db testdb1 --collection transactions --type=csv --headerline --file=/tmp/data/transacions.csv

And the file structure:

project folder:
-docker-compose.yaml
-init-mongodb:
--01-create-db.js
--02-init.sh
--data:
---transactions.csv

In project folder is docker-compose.yaml file. Scripts (both *js and *sh located in "init-mongodb" folder. CSV file located in "init-mongodb/data/" folder.

CodePudding user response:

Overall what you are doing is correct. You have a typo in the filename in your mongodbimport command in 02-init.sh. There is a "t" missing in transactions.csv

  • Related