Wondering if it is possible to run mongodump/mongorestore from within nodejs. Probably not really a mongoose thing as that is not really what mongoose is for. But wondering if there is a way to execute these command from the mongodb nodejs driver.
Looks like I can do this as a last resort: How do I execute the mongodump command programmatically from node.js?
When you say 'from node.js'
I assume you mean calling the mongodump/mongostore
command from your node.js application.
Since mongodump
and mongostore
are command line applications. The only way you can "execute them" from node.js
is to use the solution you already found here.
That is, using child_process.spawn
since no-one has created a node.js binding for them yet.
I would not use it as "a last resort" since it does exactly what you need. However, I should mention you can implement backing-up and restoring mongodb
data yourself pretty easily with the node.js driver
. That is, iterate through the collections and store them, do the reverse for restoring a back up.
I strongly suggest against it though. The problem with the approach suggested in the question you linked to is relatively minor. I've seen it used (well, something similar) in production myself and there were no issues.
I've written a tiny module to this: https://github.com/meryn/mongo-utils . It parses the mongo connection string for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With