Home > Blockchain >  How to get data from a local project csv file and store it in mongodb in docker
How to get data from a local project csv file and store it in mongodb in docker

Time:07-25

I have a scrapping python project which stored pandas data frame in a csv file. I setup docker run mongo but cant figure out how to store the data of csv file in mongodb. Do I have to copy the file in a specific location? and then try to import. I have tried mongoimport db.insert nothing seems to work

CodePudding user response:

To import CSV file to a running mongodb cluster:

mongoimport --db DB_Name --collection Collection_Name --type=csv --
headerline --file=Name-of-file-to-import

The CSV file should be local to the mongoimport command, this means if you are running this command on your local computer then the file should be local as well and you just specify path to the file filename.

If the mongoimport command is in the docker container, then you need to use docker cp command to copy the CSV file inside the container, and give the path to the CSV file in the container to the mongoimport command which will also be executed inside the container.

mongoimport command arguements:

  • DB_Name: represents the name of the database that contains the collection Collection_Name.
  • type: specifies the file type CSV (Optional field).
  • headerline: details the mongoimport command to take the 1st record of CSV file(s) as field names.
  • Name-of-file-to-import: represents the name and path of the CSV file to be imported/restored

CodePudding user response:

  • docker cp file-path docker-container-hash:/tmp
  • docker exec -it docker-container-hash bash
  • cd tmp # Because file copied is located here
  • mongoimport --db DB_Name --collection Collection_Name --type csv --headerline --file Name-of-file-to-import
  • Related