- Create bash script that will check if file is csv or json and will start mongoimport command with the necessary parameters and will move the file to other dir when is loaded.
Lets suppose the files you want to check are located inside /pathToFile/ folder and after importing the file you need to move them to /pathToImportedFiles/ .
Your bash script can start as follow:
for fname in $(find /pathToFIle/* -maxdepth 1 -type f)
do
if [[ $file == *.csv ]]; mongoimport csv ;fi;
if [[ $file == *.json ]]; mongoimport json ;fi;
done
example mongoimport json file:
mongoimport --port 27017 --db theDatabase --collection theCollection --file /pathToFile/theJsonFile.json --jsonArray
example mongoimport csv file:
mongoimport --port 27017 --db theDatabase --collection theCollection --type csv --fields "a,b,c" --file /pathToFile/theCsvFile.csv
Add the bash script to your app user crontab(crontab -e ) with the necessary periodicity.
Your crontab entry to check and load files at 05:30h every day may look as follow:
30 05 * * * /pathToScript/loader.sh
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…