mongodbbashshellmongo-shellmongoimport

How to import csv files into Mongo?


I want to import csv files into MongoDB. I found that there is a command "mongoimport" to import files.

Based on mongoimport documentation I have installed the Database Tools for Windows:

I put on PATH of Environment Variables:

C:\Program Files\MongoDB\Tools\100\bin

and then I have restarted the PC.

I have created a bash script to execute mongoimport for all the csv files into the script directory:

#!/bin/bash

HOST="00.00.000.00:00000"                                                                                                             
USERNAME="user"
PASSWORD="password"
DB_NAME="mydb"

CSV_EXTENSION=".csv"

# Get the directory path of the script
SCRIPT_DIRECTORY=$(pwd)

for fullFilePath in "$SCRIPT_DIRECTORY"/*
do
  
  filename="${fullFilePath##*/}"                                  
  
  fileNameWithoutExtension="${filename%.[^.]*}"                   

  fileExtension="${filename:${#fileNameWithoutExtension}}"        

  fileExtensionLowerCase=${fileExtension,,}
  
  if [ "$fileExtensionLowerCase" == "$CSV_EXTENSION" ]; then
  
      echo "importing collection: $fileNameWithoutExtension ..."
      
      mongoimport --db=$DB_NAME --collection=$fileNameWithoutExtension --type csv --headerline --file=$fullFilePath --host=$HOST -u=$USERNAME -p=$PASSWORD --authenticationDatabase=admin
  fi
  
done

When I execute the script I got

$ ./Import_File_On_Mongo.sh
importing collection: Test ...
2023-10-06T17:49:03.612+0200    error parsing command line options: error parsing positional arguments: provide only one file name and only one MongoDB connection string. Connection strings must begin with mongodb:// or mongodb+srv:// schemes

Solution

  • The mistake was the missing double quotes on variables (as mentioned by @LMC on comments).

    I have then added the authenticationMechanism cause I'm using SCRAM-SHA-256, and the default one is SCRAM-SHA-1.

    #!/bin/bash
    
    HOST="00.00.000.00:00000"                                                                                                             
    USERNAME="user"
    PASSWORD="password"
    DB_NAME="mydb"
    
    CSV_EXTENSION=".csv"
    
    # Get the directory path of the script
    SCRIPT_DIRECTORY="$(pwd)"
    
    for fullFilePath in "$SCRIPT_DIRECTORY"/*
    do
      
      fileName="${fullFilePath##*/}"
      
      fileNameWithoutExtension="${fileName%.[^.]*}"
      
      fileExtension="${fileName:${#fileNameWithoutExtension}}"
      fileExtensionLowerCase="${fileExtension,,}"
      
      if [ "$fileExtensionLowerCase" == "$CSV_EXTENSION" ]; then
      
          echo "importing collection: $fileNameWithoutExtension ..."
          
          mongoimport --file="$fullFilePath" --type csv --headerline --db="$DB_NAME" --collection="$fileNameWithoutExtension" --host="$HOST" -u="$USERNAME" -p="$PASSWORD" --authenticationMechanism SCRAM-SHA-256 --authenticationDatabase admin
    
      fi
      
    done