database 如何使用 mongoimport 导入 csv
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4686500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use mongoimport to import csv
提问by Joe
Trying to import a CSV with contact information:
尝试导入带有联系信息的 CSV:
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
John Doe,555 Broadway Ave,New York,NY,10010
Running this doesn't seem to add any documents to the database:
运行它似乎不会向数据库添加任何文档:
$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline
Trace says imported 1 objects
, but firing up the Mongo shell and running db.things.find()
doesn't show any new documents.
Trace 说imported 1 objects
,但是启动 Mongo shell 并运行db.things.find()
并没有显示任何新文档。
What am I missing?
我错过了什么?
回答by Robert Stewart
Your example worked for me with MongoDB 1.6.3 and 1.7.3. Example below was for 1.7.3. Are you using an older version of MongoDB?
您的示例适用于 MongoDB 1.6.3 和 1.7.3。下面的示例适用于 1.7.3。您使用的是旧版本的 MongoDB 吗?
$ cat > locations.csv
Name,Address,City,State,ZIP
Jane Doe,123 Main St,Whereverville,CA,90210
John Doe,555 Broadway Ave,New York,NY,10010
ctrl-d
$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline
connected to: 127.0.0.1
imported 3 objects
$ mongo
MongoDB shell version: 1.7.3
connecting to: test
> use mydb
switched to db mydb
> db.things.find()
{ "_id" : ObjectId("4d32a36ed63d057130c08fca"), "Name" : "Jane Doe", "Address" : "123 Main St", "City" : "Whereverville", "State" : "CA", "ZIP" : 90210 }
{ "_id" : ObjectId("4d32a36ed63d057130c08fcb"), "Name" : "John Doe", "Address" : "555 Broadway Ave", "City" : "New York", "State" : "NY", "ZIP" : 10010 }
回答by user2514493
I was perplexed with a similar problem where mongoimport did not give me an error but would report importing 0 records. I had saved my file that didn't work using the OSX Excel for Mac 2011 version using the default "Save as.." "xls as csv" without specifying "Windows Comma Separated(.csv)" format specifically. After researching this site and trying the "Save As again using "Windows Comma Separated (.csv)" format, mongoimport worked fine. I think mongoimport expects a newline character on each line and the default Mac Excel 2011 csv export didn't provide that character at the end of each line.
我对一个类似的问题感到困惑,其中 mongoimport 没有给我一个错误,但会报告导入 0 条记录。我使用默认的“另存为..”“xls as csv”保存了无法使用 OSX Excel for Mac 2011 版本的文件,而没有专门指定“Windows 逗号分隔(.csv)”格式。在研究了这个站点并尝试使用“Windows 逗号分隔 (.csv)”格式再次另存为后,mongoimport 工作正常。我认为 mongoimport 期望每一行都有一个换行符,而默认的 Mac Excel 2011 csv 导出没有提供每行末尾的字符。
回答by d.danailov
We need to execute the following command:
我们需要执行以下命令:
mongoimport --host=127.0.0.1 -d database_name -c collection_name --type csv --file csv_location --headerline
-d is database name
-c is collection name
--headerline If using --type csv or --type tsv, uses the first line as field names. Otherwise, mongoimport will import the first line as a distinct document.
-d 是数据库名称
-c 是集合名称
--headerline 如果使用 --type csv 或 --type tsv,则使用第一行作为字段名称。否则, mongoimport 会将第一行作为不同的文档导入。
For more information: mongoimport
更多信息:mongoimport
回答by blueskin
you will most likely need to authenticate if you're working in production sort of environments. You can use something like this to authenticate against the correct database with appropriate credentials.
如果您在生产环境中工作,您很可能需要进行身份验证。您可以使用类似的方法使用适当的凭据对正确的数据库进行身份验证。
mongoimport -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'
回答by R-Ling Chou
I use this on mongoimport shell
我在 mongoimport shell 上使用它
mongoimport --db db_name --collection collection_name --type csv --file C:\Your_file_path\target_file.csv --headerline
type can choose csv/tsv/json
But only csv/tsv can use --headerline
类型可以选择 csv/tsv/json 但只有 csv/tsv 可以使用 --headerline
You can read more on the offical doc.
您可以在官方文档上阅读更多内容。
回答by Maxence
Check that you have a blank line at the end of the file, otherwise the last line will be ignored on some versions of mongoimport
检查文件末尾是否有空行,否则在某些版本的 mongoimport 中最后一行将被忽略
回答by Somnath Muluk
Robert Stewart have already answered for how to import with mongoimport.
Robert Stewart 已经回答了如何使用 mongoimport 导入。
I am suggesting easy way to import CSV elegantly with 3T MongoChefTool (3.2+ version). Might help someone in future.
我建议使用3T MongoChefTool(3.2+ 版本)优雅地导入 CSV 的简单方法。将来可能会帮助某人。
- You just need to select collection
- Select file to import
- You can also unselect data which is going to import. Also many options are there.
- Collection imported
- 您只需要选择集合
- 选择要导入的文件
- 您还可以取消选择要导入的数据。还有很多选择。
- 导入的集合
查看如何导入视频
回答by user8006819
First you should come out of the mongo
shell and then execute the mongoimport
command like this:
首先你应该从mongo
shell 中出来,然后mongoimport
像这样执行命令:
Manojs-MacBook-Air:bin Aditya$ mongoimport -d marketdata -c minibars
--type csv
--headerline
--file '/Users/Aditya/Downloads/mstf.csv'
2017-05-13T20:00:41.989+0800 connected to: localhost
2017-05-13T20:00:44.123+0800 imported 97609 documents
Manojs-MacBook-Air:bin Aditya$
回答by Sreshta Tric
For the 3.4 version, please use the following syntax:
对于 3.4 版本,请使用以下语法:
mongoimport -u "username" -p "password" -d "test" -c "collections" --type csv --file myCsv.csv --headerline
After 3 days, I finally made it on my own. Thanks to all the users who supported me.
3天后,我终于自己做了。感谢所有支持我的用户。
回答by Pooja Khatri
When I was trying to import the CSV file, I was getting an error. What I have done. First I changed the header line's column names in Capital letter and removed "-" and added "_" if needed. Then Typed below command for importing CSV into mongo
当我尝试导入 CSV 文件时,出现错误。我做了什么。首先,我以大写字母更改了标题行的列名称,并删除了“-”并根据需要添加了“_”。然后键入以下命令将 CSV 导入 mongo
$ mongoimport --db=database_name --collection=collection_name --type=csv --file=file_name.csv --headerline