如何为mongodb做工作 [英] How to do jobs for mongodb

查看:78
本文介绍了如何为mongodb做工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须完成确定是否存在.json或.csv文件并将其导入Mongo数据库的工作.但是我不知道该怎么做.有人可以帮我吗?

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?

数据库配置有3个分片的Mongo分片. (TestSharding).该数据库配置了具有3个分片的Mongo分片. (TestSharding),脚本可以是bash

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

推荐答案

  1. 创建bash脚本,该脚本将检查文件是csv还是json,并使用必要的参数启动mongoimport命令,并在加载文件时将文件移至其他目录. 让我们假设您要检查的文件位于/pathToFile/文件夹中,并且在导入文件后,您需要将它们移动到/pathToImportedFiles/.
  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/ .

您的bash脚本可以按以下方式启动:

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

示例mongoimport json文件:

example mongoimport json file:

mongoimport --port 27017 --db theDatabase --collection theCollection --file /pathToFile/theJsonFile.json --jsonArray

示例mongoimport csv文件:

example mongoimport csv file:

mongoimport --port 27017 --db theDatabase --collection theCollection --type csv --fields "a,b,c" --file /pathToFile/theCsvFile.csv

  1. 以必要的周期将bash脚本添加到您的应用程序用户crontab(crontab -e). 您每天在05:30时检查和加载文件的crontab条目可能如下所示:

  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

30 05 * * * /pathToScript/loader.sh

这篇关于如何为mongodb做工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆