包含联系方式的CSV文件:

Name,Address,City,State,ZIP  
Jane Doe,123 Main St,Whereverville,CA,90210  
John Doe,555 Broadway Ave,New York,NY,10010 

运行这个不会向数据库添加文档:

$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline

Trace说导入了1个对象,但是在MongoDB shell中运行db.things.find()不会显示任何新文档。

我错过了什么?


当前回答

检查文件末尾是否有空行,否则在某些版本的mongoimport上最后一行将被忽略

其他回答

确保将.csv文件复制到/usr/local/bin或任何mondodb所在的文件夹

对于3.4版本,请使用以下语法:

mongoimport -u "username" -p "password" -d "test" -c "collections" --type csv --file myCsv.csv --headerline

3天后,我终于靠自己做到了。感谢所有支持我的用户。

使用:

mongoimport -d 'database_name' -c 'collection_name' --type csv --headerline --file filepath/file_name.csv

Robert Stewart已经回答了如何用mongoimport导入。

我建议用3T MongoChef工具(3.2+版本)简单地导入CSV。也许将来能帮到别人。

你只需要选择集合 选择要导入的文件 您还可以取消选择将要导入的数据。还有很多选择。 收集进口

查看如何导入视频

如果您有多个文件,并且希望使用python导入所有文件,您可以执行以下操作。

import os
import subprocess

# directory of files
dir_files = 'C:\data'
# create list of all files
_, _, fns = next(os.walk(dir_files))
files = [os.path.join(dir_files, fn) for fn in fns]
# mongotool address
mongotool = r'C:\Program Files\MongoDB\Server\4.4\bin\mongoimport.exe'
# name of mongodb database
mydatabase = 'mydatabase'
# name of mongodb collection
mycollection = 'mycollection'
# import all files to mongodb
for fl in files:
    commands =[mongotool, '--db', mydatabase,
               '--collection', mycollection,
               '--file', fl,
               '--type', 'tsv',
               '--headerline']
    subprocess.Popen(commands, shell=True)