mongodb 如何在mongodb中导入csv文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16417029/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 13:12:27  来源:igfitidea点击:

how to import csv file in mongodb

mongodb

提问by user2357929

I have a csv file containing following data and want to import it in mongodb

我有一个包含以下数据的 csv 文件,并希望将其导入 mongodb

ID;"AdmissionID";"SeatNo";"RegistrationNo";"ResultDate";"ResultStatusId"
1;12;"2323";"23";07-05-2013;1
2;23;"35";"32";10-05-2013;5

this data is to be imported to mongodb 2.2. I'm using following command:

此数据将导入到 mongodb 2.2。我正在使用以下命令:

mongoimport -d test -c exam --type csv --headerline <f:\exam.csv

when used i get following error

使用时出现以下错误

SyntaxError: missing ; before statement (shell):1

please help me to find out the error

请帮我找出错误

回答by flavian

This should do the trick easily. More HERE.

这应该很容易解决。更多在这里

 mongoimport -d mydb -c collectionName --type csv --file myfile.csv --headerline

Your problem is the <f:\exam.csvbit, which is not properly escaped by the way it looks

你的问题是<f:\exam.csv位,它的外观没有正确转义

> --headerline
> If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct
> document.

回答by Ambrose Pravin

Please try this line of code

请尝试这行代码

C:\Program Files\MongoDB\Server.2\bin>mongoimport -d pravin -c FOC --type csv
--file D:\Script\ImportData\FOC.csv --headerline
2016-08-10T15:42:38.685+0530    connected to: localhost`enter code here`
2016-08-10T15:42:38.758+0530    imported 13 documents

回答by Andrea D'Aguanno

I solved by opening the .csv file in Excel (File -> Options -> advanced) then unchecking the box "uses system separation" and then removing the comma from the box below and then saving again in .csv.

我通过在 Excel 中打开 .csv 文件(文件 -> 选项 -> 高级)然后取消选中“使用系统分隔”框,然后从下面的框中删除逗号,然后再次保存在 .csv 中解决了这个问题。

enter image description here

在此处输入图片说明

So there will not be any commas in the .csv file and the formatting of JSON in MongoDB will be right.

所以 .csv 文件中不会有任何逗号,并且 MongoDB 中的 JSON 格式是正确的。

回答by Krishna

Run mongoimport from the system command line, not the mongo shell.

从系统命令行运行 mongoimport,而不是 mongo shell。

Ref - https://docs.mongodb.com/manual/reference/program/mongoimport/

参考 - https://docs.mongodb.com/manual/reference/program/mongoimport/