[英]export and import data from mongodb using node.js express
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. 我想将所有数据从服务器导出到本地(我也没有备份,因此可以用作备份。因此,我使用以下代码以json格式获取集合并将其保存到本地。
I am using mongodb and node express. 我正在使用mongodb和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. 现在,我想在本地计算机中处理json,以便本地数据与服务器同步。
try to use mongodump
or mongoexport
. 尝试使用mongodump
或mongoexport
。 https://docs.mongodb.org/manual/core/backups/ https://docs.mongodb.org/manual/core/backups/
mongoexport provide efficent way to export data from collection (by query if you want) mongoexport提供了一种从集合中导出数据的有效方法(如果需要,可以通过查询)
mongodump makes a full database dump (backup) so you can easily restore it anywhere, with the same ids, references, indexes. mongodump会进行完整的数据库转储(备份),因此您可以使用相同的id,引用和索引轻松地将其还原到任何地方。 All your stuff 你所有的东西
You can use node.js child_process.exec
for running mongodump/mongoexport. 您可以使用node.js child_process.exec
来运行mongodump / mongoexport。 You can use linux crontab
for scheduling backups. 您可以使用linux crontab
计划备份。 So many ways to organize this with right tools 有很多方法可以使用正确的工具进行组织
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.