Search code examples
javamongodbmongodb-querymongoimport

How to export a mongo collection to json file from java


I want to perform the mongoexport functionality within a java program. That is I want something similar to mongoexport -db myDatabase --collection Page --out Page.json

Is there a way this can be done using java programming?


Solution

  • Runtime.getRuntime().exec("mongoexport --host host_name --port port_number --db myDatabase --collection Page --out Page.json");
    

    See Runtime, and mongodb should be added in your environmental variables.