我想将所有数据从我的服务器导出到我的本地(我也没有备份,所以这将用作备份.所以我使用下面的代码以 json 格式获取集合并保存到本地.
我正在使用 mongodb 和 node express.
dbName.find().exec(function(err, output){var jsonOutput=JSON.stringify(输出)fs.writeFile('downloads/output.json', output, function (err) {如果(错误){控制台日志(错误)res.send('错误')}别的{var 文件名 = 'res.json'var mimetype = '应用程序/json'res.setHeader('Content-disposition', 'attachment; filename=' + filename)res.setHeader('Content-type', mimetype)res.end(jsonOutput)}})})这给了我我想要的.现在我想在我的本地机器上处理json,以便我的本地数据与服务器同步.
尝试使用 mongodump 或 mongoexport.docs.mongodb/manual/core/backups/>
mongoexport 提供从集合中导出数据的有效方法(如果需要,可以通过查询)
mongodump 进行完整的数据库转储(备份),因此您可以轻松地在任何地方恢复它,使用相同的 ID、引用、索引.你所有的东西
您可以使用 node.js child_process.exec 来运行 mongodump/mongoexport.您可以使用 linux crontab 来安排备份.用正确的工具组织起来的方法有很多
I want to export all data from my server to my local(also i don't have a backup so this will serve as a backup. So I used the below code to get the collection in a json format and save to the local.
I am using mongodb and node express.
dbName.find().exec(function(err, output){ var jsonOutput=JSON.stringify(output) fs.writeFile('downloads/output.json', output, function (err) { if (err) { console.log(err) res.send('error') } else{ var filename = 'res.json' var mimetype = 'application/json' res.setHeader('Content-disposition', 'attachment; filename=' + filename) res.setHeader('Content-type', mimetype) res.end(jsonOutput) } }) })This gives me what I want. Now I want to process the json in my local machine so that my local data is in sync with the server.
解决方案
try to use mongodump or mongoexport. docs.mongodb/manual/core/backups/
mongoexport provide efficent way to export data from collection (by query if you want)
mongodump makes a full database dump (backup) so you can easily restore it anywhere, with the same ids, references, indexes. All your stuff
You can use node.js child_process.exec for running mongodump/mongoexport. You can use linux crontab for scheduling backups. So many ways to organize this with right tools
更多推荐
使用 node.js express 从 mongodb 导出和导入数据
发布评论