将csv导入MongoDB。
csv-2-mongo的Python项目详细描述
csv-2-mongo
使用python将csv导入mongodb。
安装
$ pip install csv-2-mongo
用法
Usage: csv_2_mongo.py [OPTIONS] FILE Import a csv FILE to MongoDB Options: -h, --help Show this message and exit. -v, --version Show the version and exit. -d, --database name Database name. [default: test] -c, --collection name Collection name. [default: test] -H, --host host Host name. [default: 0.0.0.0] -p, --port port Port number. [default: 27017] -t, --timeout sec Connection timeout (seconds). [default: 5] -f, --force Overwrite collection if exists. -y, --yes Automatic yes to prompts.
Note: CSV headers are required to correctly correlate MongoDB
fields
to CSVfields
.
示例
将mammals.csv
导入animals
数据库中的mammals
集合:
$ csv-2-mongo -d animals -c mammals ./mammals.csv
.................................... Connecting to 0.0.0.0:27017 .................................... Import data.csv to database=animals collection=mammals [y/N]? y Import complete!<> > {{CD3}}集合中的{^
Note: the
-f, --force
flag will overwrite the collection without a prompt. If the flag is omitted,csv-2-mongo
will prompt you before operations with potential data loss.
$ csv-2-mongo -f -d animals -c mammals ./mammals.csv
.................................... Connecting to 0.0.0.0:27017 .................................... Import data.csv to database=animals collection=mammals [y/N]? y Import complete!
将mammals.csv
导入mammals
集合,该集合位于hostmongo.travismclarke.com上运行的port27111:
$ csv-2-mongo -d animals -c mammals -H mongo.travismclarke.com -p 27111 ./mammals.csv
.................................... Connecting to mongo.travismclarke.com:27111 .................................... Import data.csv to database=animals collection=mammals [y/N]? y Import complete!
要跳过提示(例如Import data.csv to ...
),例如通过脚本运行,可以传递-y
,--yes
标志来强制yes响应确认提示:
Note: this will not suppress the overwrite prompt which still requires the
-f, --force
flag.
$ csv-2-mongo -y -d animals -c mammals ./mammals.csv
.................................... Connecting to 0.0.0.0:27017 .................................... Import complete!
许可证
麻省理工学院Travis Clarke