简体   繁体   English

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

[英]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,以便本地数据与服务器同步。

  1. How to store all the data in bulk to the backend? 如何将所有数据批量存储到后端?
  2. I have few references between the schema, so will new '_id' be created hence affecting my references 模式之间我的引用很少,因此会创建新的“ _id”,从而影响我的引用
  3. If you think this is not the right way to export the data, how can it be done using node express? 如果您认为这不是导出数据的正确方法,那么如何使用node express来完成呢?

try to use mongodump or mongoexport . 尝试使用mongodumpmongoexport 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.

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