如何使用 mongoimport 导入 CSV 文件?

发布于 2024-10-12 09:05:50 字数 407 浏览 4 评论 0原文

包含联系信息的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(23

可爱咩 2024-10-19 09:05:50

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

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?

$ 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 }
落在眉间の轻吻 2024-10-19 09:05:50

我对类似的问题感到困惑,其中 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.

偏爱你一生 2024-10-19 09:05:50

我们需要执行以下命令:

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

-d是数据库名称

-c是集合名称

--headerline 如果使用 --type csv 或 --type tsv,则使用第一行作为字段名称。否则,mongoimport 会将第一行作为不同的文档导入。

有关更多信息:mongoimport

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.

For more information: mongoimport

萝莉病 2024-10-19 09:05:50

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

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

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'
原谅我要高飞 2024-10-19 09:05:50

我在 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

您可以在 官方文档

I use this on 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

You can read more on the offical doc.

罪#恶を代价 2024-10-19 09:05:50

检查文件末尾是否有空行,否则最后一行在某些版本的 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

像你 2024-10-19 09:05:50

当我尝试导入 CSV 文件时,出现错误。我做了什么。
首先,我将标题行的列名称更改为大写字母,并删除“-”并根据需要添加“_”。然后输入以下命令将 CSV 导入 mongo

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

在此处输入图像描述

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

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

enter image description here

ζ澈沫 2024-10-19 09:05:50

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

我建议使用 3T MongoChef 工具(3.2+ 版本)优雅地导入 CSV 的简单方法。将来可能会帮助某人。

  1. 您只需要选择集合
  2. 选择要导入的文件
  3. 您也可以取消选择要导入的数据。还有很多选择。
  4. 已导入集合

请参阅如何导入视频

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.

  1. You just need to select collection
  2. Select file to import
  3. You can also unselect data which is going to import. Also many options are there.
  4. Collection imported

See how to import video

平安喜乐 2024-10-19 09:05:50

首先,您应该退出 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$

First you should come out of the mongo shell and then execute the mongoimport command like this:

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$
谜泪 2024-10-19 09:05:50

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

我想补充一点,您还可以使用 --columHaveTypes 和 --fields 键入字段,如下所示:(

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

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

对于其他类型,请参阅此处的文档: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 :

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

野生奥特曼 2024-10-19 09:05:50

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

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

经过3天,我终于自己做出来了。感谢所有支持我的用户。

For the 3.4 version, please use the following syntax:

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.

尐偏执 2024-10-19 09:05:50

我的要求是将 .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

下面是导入后的屏幕截图:

在此处输入图像描述

其中名称email.csv 文件中的列。

My requirement was to import the .csv (with no headline) to remote MongoDB instance. For mongoimport v3.0.7below command worked for me:

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:

enter image description here

where name and email are the columns in the .csv file.

只怪假的太真实 2024-10-19 09:05:50

给定 .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 文件中的列的 >。

Given .csv file I have which has only one column with no Header, below command worked for me:

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.

一笑百媚生 2024-10-19 09:05:50

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

瀞厅☆埖开 2024-10-19 09:05:50

只需在执行 mongoimport 后使用它,

它将返回导入的对象

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

数量。

Just use this after executing mongoimport

It will return number of objects imported

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

will return the number of objects.

是你 2024-10-19 09:05:50

使用 :

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

use :

mongoimport -d 'database_name' -c 'collection_name' --type csv --headerline --file filepath/file_name.csv
江湖彼岸 2024-10-19 09:05:50

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

检查收集数据:-

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
}

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

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
}

晨光如昨 2024-10-19 09:05:50
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)
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)
我的痛♀有谁懂 2024-10-19 09:05:50

奇怪的是没有人提到 --uri 标志:

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

Strangely no one mentioned --uri flag:

mongoimport --uri connectionString -c questions --type csv --file questions.csv --headerline 
债姬 2024-10-19 09:05:50

与未来的读者分享:

在我们的例子中,我们需要添加 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

Sharing for future readers:

In our case, we needed to add the host parameter to make it work

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
请别遗忘我 2024-10-19 09:05:50

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

Make sure to copy the .csv file to /usr/local/bin or whatever folder your mondodb is in

平定天下 2024-10-19 09:05:50

上面所有这些答案都很棒。以及开发全功能应用程序的方法。

但是,如果您想要快速构建原型,想要灵活性(因为集合仍然会变化)以及最小化您的早期代码库,那么有一种更简单的方法,这里没有太多讨论。

现在你基本上可以放弃 mongoimport 了。如果在这个问题上提到的话,我可以节省 3 个小时。所以让我分享给其他人:

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

  1. 您只需让 MongoDB compass 运行在按照简单的安装即可安装到您的机器上。直接在 GUI 中用于数据库连接和身份验证的几个字段。
  2. 导入 csv/json 文件。在用户(我)进行验证之前,解析 30KB 文件只需要不到一秒的时间。
  3. 验证每个属性的“类型”。很棒的功能,我可以直接提及布尔值、整数等属性类型。根据我的经验,它们似乎都默认为字符串。您可以在导入之前进行更新。日期更加挑剔,需要在编码方面特别注意。
  4. 再单击一次,csv 就是本地 mongo 数据库或云端的集合。瞧!

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

  1. 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.
  2. Import the csv/json file. It took less than a second on a 30KB file to be parsed before user (me) validates.
  3. 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.
  4. One click further the csv is a collection in your mongo db local or on the cloud. Voila!
猥琐帝 2024-10-19 09:05:50

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

If you have multiple files and you want to import all of them using python, you can do the following.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文