0

我正在尝试编写接收包含已保存 MongoDB 集合的 zip 文件的 Node 类,然后读取它yauzl并将这些集合恢复到本地 DB 数据集。收集档案是使用创建的mongodump,每个文件都有 filename <db-name>.<collection-name>.archive

yauzl.open(zipFile, {lazyEntries: true}, function(err, zip) {
        if (err) throw err;
        if (zip) {
            zip.readEntry();
            zip.on("entry", function (entry) {
                zip.openReadStream(entry, function (err, readStream) {
                    if (err) throw err;
                    if (readStream) {
                        console.log(`Uploading ${entry.fileName}`)
                        const nameParts = entry.fileName.split('.');
                        const collName = nameParts[1];
                        const dbName = nameParts[0];
                        const params = [
                             `--uri="mongodb://localhost:27017"`,
                            `--nsFrom="${dbName}.${collName}"`,
                            `--nsTo="localDB.${collName}"`,
                            `--archive`
                        ];

                        const child = child_process.spawn('mongorestore', params);
                        child.stderr.setEncoding('utf8')
                        child.stderr.on('data', (err: string) => {
                           console.log(`mongorestore: ${err && err.trim()}`);
                        })

                        readStream.on('data', (data) => {
                            child.stdin.write(data);
                        })
                        readStream.on("end", function () {
                            zip.readEntry();
                        });
                    }
                });
            });
            zip.once('end', async () => {
                console.log('Success.');
                process.exit(0);
            })
        }
    });

这主要适用 - 对于小型集合,文件被读取和恢复。但是对于大型集合 (1GB) 数据,它只恢复一个 30 Mb 的块,然后继续到下一个文件。似乎在 CLI 中恢复大型档案时,档案以 30Mb 的块上传(您可以在终端中看到它,它会写入其进度)。当我运行以下代码时,在第一个块之后on('end')被调用并且读取流关闭。

任何想法这里缺少什么?

4

0 回答 0