[英]How to use mongoimport to import csv
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?我错过了什么?
Your example worked for me with MongoDB 1.6.3 and 1.7.3.您的示例适用于 MongoDB 1.6.3 和 1.7.3。 Example below was for 1.7.3.
下面的示例适用于 1.7.3。 Are you using an older version of MongoDB?
您使用的是旧版本的 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 }
I was perplexed with a similar problem where mongoimport did not give me an error but would report importing 0 records.我对一个类似的问题感到困惑,其中 mongoimport 没有给我一个错误,但会报告导入 0 条记录。 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.
我使用默认的“另存为..”“xls as csv”保存了无法使用 OSX Excel for Mac 2011 版本的文件,而没有专门指定“Windows 逗号分隔(.csv)”格式。 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.
在研究了这个站点并尝试使用“Windows 逗号分隔 (.csv)”格式再次另存为后,mongoimport 工作正常。我认为 mongoimport 期望每行有一个换行符,而默认的 Mac Excel 2011 csv 导出没有提供每行末尾的字符。
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
-d 是数据库名称
-c is collection name
-c 是集合名称
--headerline If using --type csv or --type tsv, uses the first line as field names.
--headerline 如果使用 --type csv 或 --type tsv,则使用第一行作为字段名称。 Otherwise, mongoimport will import the first line as a distinct document.
否则, mongoimport 会将第一行作为不同的文档导入。
For more information: mongoimport更多信息: 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 -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'
检查文件末尾是否有空行,否则在某些版本的 mongoimport 中最后一行将被忽略
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
type 可以选择 csv/tsv/json 但只有 csv/tsv 可以使用
--headerline
You can read more on the offical doc .您可以在 官方文档上阅读更多内容。
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 MongoChef Tool (3.2+ version).我建议使用3T MongoChef Tool(3.2+ 版本)优雅地导入 CSV 的简单方法。 Might help someone in future.
将来可能会帮助某人。
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$
Robert Stewart's answers is great.罗伯特·斯图尔特的回答很棒。
I'd like to add that you also can type your fields with --columHaveTypes and --fields like this :我想补充一点,您还可以使用 --columHaveTypes 和 --fields 键入您的字段,如下所示:
mongoimport -d myDb -c myCollection --type csv --file myCsv.csv
--columnsHaveTypes --fields "label.string(),code.string(),aBoolean.boolean()"
(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对于其他类型,请参阅此处的文档: https : //docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-columnshavetypes
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. 3天后,我终于自己做了。 Thanks to all the users who supported me.
感谢所有支持我的用户。
My requirement was to import the .csv (with no headline)
to remote MongoDB
instance.我的要求是将
.csv (with no headline)
导入到远程MongoDB
实例。 For mongoimport v3.0.7
below command worked for me:对于
mongoimport v3.0.7
下面的命令对我mongoimport v3.0.7
:
mongoimport -h <host>:<port> -u <db-user> -p <db-password> -d <database-name> -c <collection-name> --file <csv file location> --fields <name of the columns(comma seperated) in csv> --type csv
For example:例如:
mongoimport -h 1234.mlab.com:61486 -u arpitaggarwal -p password -d my-database -c employees --file employees.csv --fields name,email --type csv
Below is the screenshot of how it looks like after import:下面是导入后的截图:
where name
and email
are the columns in the .csv
file.其中
name
和email
是.csv
文件中的列。
Given .csv
file I have which has only one column with no Header , below command worked for me:鉴于我只有一列没有 Header 的
.csv
文件,以下命令对我有用:
mongoimport -h <mongodb-host>:<mongodb-port> -u <username> -p <password> -d <mongodb-database-name> -c <collection-name> --file file.csv --fields <field-name> --type csv
where field-name refers to the Header name of the column in .csv
file.其中field-name 是指
.csv
文件中列的标题名称。
When I was trying to import the CSV file, I was getting an error.当我尝试导入 CSV 文件时,出现错误。 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 导入 mongo
$ mongoimport --db=database_name --collection=collection_name --type=csv --file=file_name.csv --headerline
C:\\wamp\\mongodb\\bin>mongoexport --db proj_mmm --collection 产品 --csv --fieldFile 产品_fields.txt --out 产品.csv
使用:
mongoimport -d 'database_name' -c 'collection_name' --type csv --headerline --file filepath/file_name.csv
check collection data:-检查收集数据:-
var collections = db.getCollectionNames(); for(var i = 0; i< collections.length; i++) { print('Collection: ' + collections[i]); // print the name of each collection db.getCollection(collections[i]).find().forEach(printjson); //and then print the json of each of its elements }
1]We can save xsl as .csv file
2] Got to MongoDB bin pathon cmd - > cd D:\Arkay\soft\MongoDB\bin
3] Run below command
> mongoimport.exe -d dbname -c collectionname --type csv --file "D:\Arkay\test.csv" --headerline
4] Verify on Mongo side using below coomand.
>db.collectioname.find().pretty().limit(1)
奇怪的是没有人提到--uri
标志:
mongoimport --uri connectionString -c questions --type csv --file questions.csv --headerline
Sharing for future readers:
分享给未来的读者:
In our case, we needed to add the host
parameter to make it work在我们的例子中,我们需要添加
host
参数以使其工作
mongoimport -h mongodb://someMongoDBhostUrl:somePORTrunningMongoDB/someDB -d someDB -c someCollection -u someUserName -p somePassword --file someCSVFile.csv --type csv --headerline --host=127.0.0.1
确保将 .csv 文件复制到 /usr/local/bin 或您的 mondodb 所在的任何文件夹
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.你现在基本上可以放弃 mongoimport 了。 I could have saved 3 hours if it was mentioned here on this question.
如果在这里提到这个问题,我本可以节省 3 个小时。 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. Mongodb 有一个名为Mongo Compass的 GUI,只需点击一下,即可开箱即用地提供csv 和 json 导入功能。 It is an official part of the Mongo ecosytem.
它是 Mongo 生态系统的正式组成部分。 At the time of writing it is free and it works very well for my use case.
在撰写本文时,它是免费的,并且非常适合我的用例。 https://www.mongodb.com/products/compass
https://www.mongodb.com/products/compass
If you have multiple files and you want to import all of them using python, you can do the following.如果您有多个文件,并且想使用 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)
Just use this after executing mongoimport执行 mongoimport 后使用它
It will return number of objects imported它将返回导入的对象数
use db
db.collectionname.find().count()
will return the number of objects.将返回对象的数量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.