Search code examples
phpapachemongodbstdoutmongodump

Pipe MongoDB mongodump to download


On the Linux terminal, I can do a mongodump to stdout using this command:

mongodump --db local --collection allnews --out -

But how do I get this output, and pipe it to a file that then downloads in the browser..?

I have an Apache PHP setup, with the Mongo PHP extension installed.

In PhpMyAdmin for MySQL, for example, you can do an "export", and it pipes the output to a .SQL file.

I want the same feature, but for MongoDB rather than MySQL.

Thanks

The reason I can't do a mongodump to a file is because hard drive space on the server is low.


Solution

  • You could try using passthru combined with proper headers to start download:

    <?php
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="dump.txt"');
    passthru('mongodump --db local --collection allnews --out -')