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
추적은 imported 1 objects
이지만 Mongo 셸을 실행하고 실행 db.things.find()
해도 새로운 문서가 표시되지 않습니다.
내가 무엇을 놓치고 있습니까?
귀하의 예는 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가 오류를주지 않았지만 0 레코드 가져 오기를보고하는 비슷한 문제로 어려움을 겪었습니다. "Windows Comma Separated (.csv)"형식을 구체적으로 지정하지 않고 기본 "Save as .." "xls as csv"를 사용하여 Mac 2011 용 OSX Excel 버전에서 작동하지 않는 파일을 저장했습니다. 이 사이트를 조사하고 ""Windows Comma Separated (.csv) "형식을 사용하여"다른 이름으로 저장 "을 시도한 후 mongoimport가 제대로 작동했습니다. mongoimport는 각 줄마다 줄 바꿈 문자가 필요하고 기본 Mac Excel 2011 csv 내보내기는이를 제공하지 않았다고 생각합니다 각 줄의 끝에 문자.
다음 명령을 실행해야합니다.
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
프로덕션 환경에서 작업하는 경우 인증이 필요할 것입니다. 이와 같은 것을 사용하여 적절한 자격 증명으로 올바른 데이터베이스를 인증 할 수 있습니다.
mongoimport -d db_name -c collection_name --type csv --file filename.csv --headerline --host hostname:portnumber --authenticationDatabase admin --username 'iamauser' --password 'pwd123'
파일 끝에 빈 줄이 있는지 확인하십시오. 그렇지 않으면 일부 버전의 mongoimport에서 마지막 줄이 무시됩니다.
mongoimport 쉘에서 이것을 사용합니다.
mongoimport --db db_name --collection collection_name --type csv --file C:\\Your_file_path\target_file.csv --headerline
유형은 csv / tsv / json을 선택할 수 있지만 csv / tsv 만 사용할 수 있습니다 --headerline
공식 문서 에 대한 자세한 내용을 읽을 수 있습니다 .
Robert Stewart는 이미 mongoimport로 가져 오는 방법에 대해 답변했습니다.
3T MongoChef Tool ( 3.2+ 버전)을 사용 하여 CSV를 우아하게 가져올 수있는 쉬운 방법을 제안 합니다. 나중에 누군가를 도울 수 있습니다.
- 당신은 단지 컬렉션을 선택해야합니다
- 가져올 파일을 선택하십시오
- 가져올 데이터를 선택 취소 할 수도 있습니다. 또한 많은 옵션이 있습니다.
- 가져온 컬렉션
먼저 mongo
쉘에서 나온 후 다음 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의 답변은 훌륭합니다.
또한 --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
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.
C:\wamp\mongodb\bin>mongoexport --db proj_mmm --collection offerings --csv --fieldFile offerings_fields.txt --out offerings.csv
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.
use :
mongoimport -d 'database_name' -c 'collection_name' --type csv --headerline --file filepath/file_name.csv
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
}
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)
Strangely no one mentioned --uri
flag:
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
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
My requirement was to import the .csv (with no headline)
to remote MongoDB
instance. For mongoimport v3.0.7
below 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:
where name
and email
are the columns in the .csv
file.
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.
참고URL : https://stackoverflow.com/questions/4686500/how-to-use-mongoimport-to-import-csv
'IT' 카테고리의 다른 글
부트 스트랩 그리드 시스템이있는 중첩 된 행? (0) | 2020.05.20 |
---|---|
기록에서 삭제하지 않고 변경 사항 포기 (0) | 2020.05.20 |
WPF 차트 컨트롤 (0) | 2020.05.20 |
HTML-줄임표가 활성화 된 경우에만 툴팁을 표시하는 방법 (0) | 2020.05.20 |
CLOCK_REALTIME과 CLOCK_MONOTONIC의 차이점은 무엇입니까? (0) | 2020.05.20 |