I'm able to export an entire collection using Mongoexport command in the Mongo shell.
But, I'm trying to write a java program which uses a Mongoexport command to export entire collection of MongoDB into a CSV file.
My code:
public class MongoExportSample {
public static void main(String[] args) {
String db = "pack";
String col = "col";
String Host="localhost";
String Port="27017";
String fileName = "D:/user/sample.csv";
String command = "mongoexport --host Host --port Port --db " + db + " --collection " + col + " --csv --out " + fileName + "";
try {
Process process=Runtime.getRuntime().exec(command);
int waitFor = process.waitFor();
System.out.println("waitFor:: "+waitFor);
BufferedReader success=new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader error=new BufferedReader(new InputStreamReader(process.getErrorStream()));
String s="";
while ((s = success.readLine()) != null) {
System.out.println(s);
}
while ((s = error.readLine()) != null) {
System.out.println("Std ERROR : " + s);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
I'm facing java.io.IOException: Cannot run program "mongoexport": CreateProcess error=2, The system cannot find the file specified.
Can anyone please help me out regarding the same ...