简体   繁体   English

Mongoose 将 MongoDB 文档导出和导入文件

[英]Mongoose Export and Import MongoDB Documents to/from File

Using JavaScript and Mongoose, I want to export all of the documents of a certain document class to a file (eg, a JSON or CSV file), and I also want to import (ie, "load") the contents of such a file into a database.使用 JavaScript 和 Mongoose,我想将某个文档类的所有文档导出到一个文件(例如,JSON 或 CSV 文件),并且我还想导入(即“加载”)这样一个文件的内容入数据库。 How can I accomplish this?我怎样才能做到这一点?

From mongoDB docs:来自 mongoDB 文档:

EXPORT / BACKUP导出/备份

To backup data from a mongod instance running on the same machine and on the default port of 27017, use the following command:要从运行在同一台机器上且默认端口 27017 上的 mongod 实例备份数据,请使用以下命令:

mongodump

You can also specify the --host and --port of the MongoDB instance that the mongodump should connect to.您还可以指定 mongodump 应连接到的 MongoDB 实例的 --host 和 --port。 For example:例如:

mongodump --host=mongodb.example.net --port=27017

mongodump will write BSON files that hold a copy of data accessible via the mongod listening on port 27017 of the mongodb.example.net host. mongodump 将写入 BSON 文件,其中包含可通过 mongod 侦听 mongodb.example.net 主机的端口 27017 访问的数据副本。 See Create Backups from Non-Local mongod Instances for more information.有关更多信息,请参阅从非本地 mongod 实例创建备份。

To specify a different output directory, you can use the --out or -o option:要指定不同的输出目录,您可以使用 --out 或 -o 选项:

mongodump --out=/data/backup/

IMPORT / RESTORE导入/恢复

To use mongorestore to connect to an active mongod, use a command with the following prototype form:要使用 mongorestore 连接到活动的 mongod,请使用具有以下原型形式的命令:

mongorestore --port=<port number> <path to the backup>

Consider the following example:考虑以下示例:

mongorestore dump-2013-10-25/

MongoDB Docs: Check MongoDB 文档: 检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM