包含联系方式的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上最后一行将被忽略


您的示例适用于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 }

只需在执行mongoimport后使用它

它将返回导入对象的数量

use db
db.collectionname.find().count()

将返回对象的数目。


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.


C:\wamp\mongodb\bin>mongoexport——db proj_mmm——collection offerings——csv——fieldFile offerings_fields.txt——out offerings_csv


我们需要执行以下命令:

mongoimport --host=127.0.0.1 -d database_name -c collection_name --type csv --file csv_location --headerline

-d是数据库名称 -c是集合名 如果使用——type csv或——type tsv,则使用第一行作为字段名。否则,mongoimport将第一行作为一个单独的文档导入。

欲了解更多信息:mongoimport


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

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

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

查看如何导入视频


使用:

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

如果您在生产环境中工作,则很可能需要进行身份验证。您可以使用类似的方法对具有适当凭证的正确数据库进行身份验证。

mongoimport -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'

首先你应该跳出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$

mongoimport -d test -c test——type csv——file SampleCSVFile_119kb.csv——headerline .csv

检查采集数据:—

var collections = db.getCollectionNames(); For (var I = 0;我< collections.length;我+ +) { print('Collection: ' + collections[i]); //打印每个集合的名称 db.getCollection(集合[我]);().forEach (printjson); //然后打印每个元素的json }


罗伯特·斯图尔特的回答很好。

我想补充的是,你也可以输入你的字段——columHaveTypes和——字段像这样:

mongoimport -d myDb -c myCollection --type csv --file myCsv.csv 
  --columnsHaveTypes --fields "label.string(),code.string(),aBoolean.boolean()"

(注意字段之间的逗号后不要有空格)

对于其他类型,请参阅doc: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption-mongoimport-columnshavetypes


我在mongoimport shell上使用这个

mongoimport --db db_name --collection collection_name --type csv --file C:\\Your_file_path\target_file.csv --headerline

类型可以选择csv/tsv/json 但是只有csv/tsv可以使用——headerline

你可以阅读更多的官方文件。


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)

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

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

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


奇怪的是,没有人提到——uri flag:

mongoimport --uri connectionString -c questions --type csv --file questions.csv --headerline 

分享给未来的读者:

在本例中,我们需要添加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(没有标题)到远程MongoDB实例。对于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

例如:

mongoimport -h 1234.mlab.com:61486 -u arpitaggarwal -p password  -d my-database -c employees --file employees.csv --fields name,email --type csv

下面是导入后的截图:

其中姓名和电子邮件是.csv文件中的列。


给定.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

其中field-name指的是.csv文件中列的Header名称。


当我试图导入CSV文件时,我得到了一个错误。我所做的。 首先,我用大写字母更改了标题行的列名,并删除了“-”,并在需要时添加了“_”。然后输入下面的命令导入CSV到mongo

$ mongoimport --db=database_name --collection=collection_name --type=csv --file=file_name.csv --headerline  


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


以上这些答案都很棒。这也是开发一个功能齐全的应用程序的方法。

但是如果你想要快速创建原型,想要在集合不断变化时保持灵活性,以及最小化你早期的代码库,有一种更简单的方法,但很少讨论。

你现在基本上可以放弃蒙古进口了。如果在这里提到这个问题,我可以节省3个小时。让我分享给大家:

Mongodb有一个名为Mongo Compass的GUI,它有csv和json导入功能,只需点击即可开箱即用。它是Mongo生态系统的正式组成部分。在撰写本文时,它是免费的,并且非常适合我的用例。 https://www.mongodb.com/products/compass

You simply get MongoDB compass running on your machine by following the simple installation. A couple of fields for DB connection and authentication directly in the GUI. Import the csv/json file. It took less than a second on a 30KB file to be parsed before user (me) validates. Validate the "type" of each property. Great feature, I could directly mention the property types such as booleans, integers, etc. In my experience, they seem all default to string. You can update before importing. Dates were more finicky and needed special attention on the coding side. One click further the csv is a collection in your mongo db local or on the cloud. Voila!


如果您有多个文件,并且希望使用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)