如何使用 mongodb java 驱动程序 import/export MongoDB 数据库的所有集合?

How to import/export all collections of the MongoDB database using mongodb java driver?

是否有使用 java 驱动程序导入和导出 mongodb 数据库的所有集合的功能。?就像有 mongodumpmongorestore 使用命令提示符。

简短的回答是否定的。这些命令只能从命令行调用。 您可能会考虑从所有集合中获取所有数据,但预计速度会很慢。

您可以阅读围绕此的讨论here

希望对您有所帮助

到目前为止,Mongo Java 驱动程序不支持此功能。

您可以尝试从 Java 运行时调用 mongoimport 和 mongorestore 命令。喜欢

Runtime.getRuntime().exec("mongoimport -d <dbname> -h <>..");
mongodump --host localhost --port 27017 --db sample    

它与文件夹精确为数据库名称示例的转储

mongorestore --db sample --verbose d:/dump/sample/

(与 here 相同的答案)

最近我开始了一个名为 mongodbdump-java-wrapper 的项目来包装 mongodump.exemongorestore.exe mongodb 来自 java 的可执行文件。

您可以从以下位置克隆它:github project。该项目包括集成测试(一种了解如何实施的方法 backup/restore)。