Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

How to do jobs for mongodb

I have to do a job that determines if there is a .json or .csv file and imports it into a Mongo database. But I don't know how to do a job. Can somebody help me?

The database is configured with a Mongo sharding with 3 Shards. (TestSharding). The database is configured with a Mongo sharding with 3 Shards. (TestSharding) and the script can be bash

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
  1. 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
  1. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...