如何使用 mongoimport 导入 CSV 文件?
包含联系信息的 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()
不显示任何新文档。
我缺少什么?
CSV file with contact information:
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 add documents to the database:
$ mongoimport -d mydb -c things --type csv --file locations.csv --headerline
Trace says imported 1 objects
, but in the MongoDB shell running db.things.find()
doesn't show any new documents.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(23)
您的示例适用于 MongoDB 1.6.3 和 1.7.3。下面的示例适用于 1.7.3。您使用的是旧版本的 MongoDB 吗?
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?
我对类似的问题感到困惑,其中 mongoimport 没有给我错误,但会报告导入 0 条记录。我使用默认的“另存为..”“xls as csv”保存了无法使用 OSX Excel for Mac 2011 版本运行的文件,而没有专门指定“Windows 逗号分隔 (.csv)”格式。研究此网站并尝试“使用“Windows 逗号分隔 (.csv)”格式再次另存为后,mongoimport 工作正常。我认为 mongoimport 期望每行有一个换行符,而默认的 Mac Excel 2011 csv 导出没有提供该功能每行末尾的字符。
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
We need to execute the following command:
For more information: mongoimport
如果您在生产环境中工作,您很可能需要进行身份验证。您可以使用类似的方法通过适当的凭据对正确的数据库进行身份验证。
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 上使用这个 shell
类型可以选择 csv/tsv/json
但只有 csv/tsv 可以使用
--headerline
您可以在 官方文档。
I use this on mongoimport shell
type can choose csv/tsv/json
But only csv/tsv can use
--headerline
You can read more on the offical doc.
检查文件末尾是否有空行,否则最后一行在某些版本的 mongoimport 上将被忽略
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
当我尝试导入 CSV 文件时,出现错误。我做了什么。
首先,我将标题行的列名称更改为大写字母,并删除“-”并根据需要添加“_”。然后输入以下命令将 CSV 导入 mongo
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
Robert Stewart 已经回答了如何使用 mongoimport 导入。
我建议使用 3T MongoChef 工具(3.2+ 版本)优雅地导入 CSV 的简单方法。将来可能会帮助某人。
请参阅如何导入视频
Robert Stewart have already answered for how to import with mongoimport.
I am suggesting easy way to import CSV elegantly with 3T MongoChef Tool (3.2+ version). Might help someone in future.
See how to import video
首先,您应该退出 mongo shell,然后执行 mongoimport 命令,如下所示:
First you should come out of the
mongo
shell and then execute themongoimport
command like this:罗伯特·斯图尔特的回答很棒。
我想补充一点,您还可以使用 --columHaveTypes 和 --fields 键入字段,如下所示:(
注意字段之间的逗号后不要有任何空格)
对于其他类型,请参阅此处的文档:https://docs.mongodb.com/manual/reference/program/mongoimport /#cmdoption-mongoimport-columnshavetypes
Robert Stewart's answers is great.
I'd like to add that you also can type your fields with --columHaveTypes and --fields like this :
(Careful to not have any space after the comma between your fields)
For other types, see doc here : https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-columnshavetypes
对于3.4版本,请使用以下语法:
经过3天,我终于自己做出来了。感谢所有支持我的用户。
For the 3.4 version, please use the following syntax:
After 3 days, I finally made it on my own. Thanks to all the users who supported me.
我的要求是将
.csv(无标题)
导入到远程MongoDB
实例。对于mongoimport v3.0.7
,下面的命令对我有用:例如:
下面是导入后的屏幕截图:
其中
名称
和email
是.csv
文件中的列。My requirement was to import the
.csv (with no headline)
to remoteMongoDB
instance. Formongoimport v3.0.7
below command worked for me:For example:
Below is the screenshot of how it looks like after import:
where
name
andemail
are the columns in the.csv
file.给定
.csv
文件,其中只有一列,没有标题,以下命令对我有用:其中 field-name 指标题名称
.csv
文件中的列的 >。Given
.csv
file I have which has only one column with no Header, below command worked for me:where field-name refers to the Header name of the column in
.csv
file.C:\wamp\mongodb\bin>mongoexport --db proj_mmm --collection 产品 --csv --fieldFile 产品_fields.txt --out 产品.csv
C:\wamp\mongodb\bin>mongoexport --db proj_mmm --collection offerings --csv --fieldFile offerings_fields.txt --out offerings.csv
只需在执行 mongoimport 后使用它,
它将返回导入的对象
数量。
Just use this after executing mongoimport
It will return number of objects imported
will return the number of objects.
使用 :
use :
mongoimport -d test -c test --type csv --file SampleCSVFile_119kb.csv --headerline
检查收集数据:-
mongoimport -d test -c test --type csv --file SampleCSVFile_119kb.csv --headerline
check collection data:-
奇怪的是没有人提到
--uri
标志:Strangely no one mentioned
--uri
flag:在我们的例子中,我们需要添加
host
参数才能使其工作In our case, we needed to add the
host
parameter to make it work确保将 .csv 文件复制到 /usr/local/bin 或 mondodb 所在的任何文件夹
Make sure to copy the .csv file to /usr/local/bin or whatever folder your mondodb is in
上面所有这些答案都很棒。以及开发全功能应用程序的方法。
但是,如果您想要快速构建原型,想要灵活性(因为集合仍然会变化)以及最小化您的早期代码库,那么有一种更简单的方法,这里没有太多讨论。
现在你基本上可以放弃 mongoimport 了。如果在这个问题上提到的话,我可以节省 3 个小时。所以让我分享给其他人:
Mongodb 有一个名为 Mongo Compass 的 GUI,只需点击一下即可开箱即用的 csv 和 json 导入功能。它是 Mongo 生态系统的官方组成部分。在撰写本文时,它是免费的,并且非常适合我的用例。
https://www.mongodb.com/products/compass
All these answers above are great. And the way to go on a full featured application.
But if you want to prototype fast, want flexibility as the collection still changes as well as to minimize your early code base, there is a much simpler way that is not much discussed.
You can basically forego mongoimport by now. I could have saved 3 hours if it was mentioned here on this question. So let me share for others:
Mongodb has a GUI called Mongo Compass has both csv and json import features out of the box in a matter of clicks. It is an official part of the Mongo ecosytem. At the time of writing it is free and it works very well for my use case.
https://www.mongodb.com/products/compass
如果您有多个文件并且想要使用 python 导入所有文件,可以执行以下操作。
If you have multiple files and you want to import all of them using python, you can do the following.