【发布时间】:2017-05-17 19:58:35
【问题描述】:
我可以在 Mongo shell 中使用 Mongoexport 命令导出整个集合。
但是,我正在尝试编写一个 java 程序,它使用 Mongoexport 命令将整个 MongoDB 集合导出到 CSV 文件中。
我的代码:
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();
}
}
}
我遇到 java.io.IOException: Cannot run program "mongoexport": CreateProcess error=2, The system cannot find the file specified.
谁能帮我解决同样的问题...
【问题讨论】:
-
您使用的是 Mongo 3.4 吗?只是为了改进导出命令的语法。
-
我使用的是 Mongo 3.2