mongoexport 명령어를 이용하면 간단하게 MongoDB 내 컬렉션의 데이터를 파일로 추출할 수 있다.
자주 사용되는 옵션:
--host <hostname><:port>, -h <hostname><:port>
--port <port>
--username <username>, -u <username>
--password <password>, -p <password>
--db <database>, -d <database>
--collection <collection>, -c <collection>
--fields <field1[,field2]>, -f <field1[,field2]>
--type <string>
--out <file>, -o <file>
데이터 추출은 JSON 또는 CSV 파일로 가능하다. (Default : JSON)
CSV 파일로 데이터를 추출하고자 할 경우 --out <file> 에서 파일명을 .csv로 끝나는 이름으로 지정해 주어야 한다.
mongoexport --db users --collection contacts --type=csv
--fields name,address --out /opt/backups/contacts.csv
위 예제와 같이 명령어를 실행했을 때, users DB 내 contacts 컬렉션의 name, address 필드를 조회한 데이터가 /opt/backups/contacts.csv 파일로 추출된다.
여기서 --fields 옵션 뒤에 필드명을 기입할 때 공백이 들어가면 안된다는 점을 주의해야 한다.
예제 출처 : https://docs.mongodb.com/manual/reference/program/mongoexport/