使用 node.js express 从 mongodb 导出和导入数据

编程入门 行业动态 更新时间:2024-10-22 20:24:42
本文介绍了使用 node.js express 从 mongodb 导出和导入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将所有数据从我的服务器导出到我的本地(我也没有备份,所以这将用作备份.所以我使用下面的代码以 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.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.

  • 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. 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 导出和导入数据

    本文发布于:2023-11-22 19:20:19,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1618688.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:数据   js   node   mongodb   express

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!