in a current MERN stack project (github repo) I am trying to implement a mechanism that resets two documents in my mongo db to default data once a day at midnight to reset changes made by others to the live website of the project. For this I use the mongodb cli database tools to import the corresponding two .json files with the respective data using the command mongoimport
.
code excerpt from db.js:
const Comment = require('../models/commentModel');
const User = require('../models/userModel');
const restoreDefaultData = async () => {
const { spawn } = require('child_process');
const restoreCollection = (collectionName) => {
const restoreProcess = spawn('mongoimport', [
`--uri="${process.env.MONGO_URI}"`,
`--collection=${collectionName}`,
`--file=./.mongodb/default-data/${collectionName}.json`,
'--jsonArray'
]);
restoreProcess.on('exit', (code, signal) => {
// handle success and error messages
});
};
// first delete all comment data and then restore the default comment data
await Comment.deleteMany({});
restoreCollection('comments');
// first delete all user data and then restore the default user data
await User.deleteMany({});
restoreCollection('users');
};
In my local project this works without problems, because there are also the mongodb cli database tools installed. To provide the live website I use railway.
After the building process started with the command..
npm i --prefix frontend && npm run build --prefix frontend
..I now get the following error in the building console:
node:events:491
throw er; // Unhandled 'error' event
^
Error: spawn mongoimport ENOENT
at Process.ChildProcess._handle.onexit (node:internal/child_process:285:19)
at onErrorNT (node:internal/child_process:485:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Emitted 'error' event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
at onErrorNT (node:internal/child_process:485:16)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -2,
code: 'ENOENT',
syscall: 'spawn mongoimport',
path: 'mongoimport',
spawnargs: [
'--uri="mongodb+srv://admin:i8myuWN4tX9yopvO@cluster0.sksfeun.mongodb.net/app?retryWrites=true&w=majority"', '--collection=comments',
'--collection=comments',
'--file=./.mongodb/default-data/comments.json',
'--jsonArray'
]
}
How do I manage to use the mongodb cli database tools in my railway deployment? Or is there even another way to reset the two documents of the database daily to certain default data?
I already tried to include the download and installation process of the mongodb cli database tools in the railway build command but this ended up in an error as well.
Am slowly a little clueless and look forward to any answer!