使用 node.js express 从 mongodb 导出和导入数据 [英] export and import data from mongodb using node.js express
问题描述
我想将所有数据从我的服务器导出到我的本地(我也没有备份,所以这将用作备份.所以我使用下面的代码以 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,以便我的本地数据与服务器同步.
- 如何将所有数据批量存储到后端?
- 我在架构之间的引用很少,所以会创建新的_id"因此影响了我的参考
- 如果您认为这不是导出数据的正确方式,如何可以使用 node express 完成吗?
尝试使用 mongodump
或 mongoexport
.https://docs.mongodb.org/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.
- How to store all the data in bulk to the backend?
- I have few references between the schema, so will new '_id' be created hence affecting my references
- If you think this is not the right way to export the data, how can it be done using node express?
try to use mongodump
or mongoexport
. https://docs.mongodb.org/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 导出和导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!