Running arbitrary MongoDB scripts through Node.js driver - node.js

I have a combination of complex MongoDB scripts that runs from the command line as follows:
$ mongo mydb config.js task.js
Since I can't run shell scripts in my server environment and need to schedule the above task, I figured I could simply concatenate the above .js files and then run them from a Node script. Hence I am looking for an equavalent to:
db.runMyCustomRawCommands(string commands)
How can I do this, or what would be an alternative solution?

To quote Christian Kvalheim, original author of the Node.js native MongoDB driver:
that's not possible as the shell is synchronous and have different apis than the node.js driver. you would have to rewrite the scripts to work for node.js.

The problem is that db.runMyCustomRawCommands is not a raw command. The drivers communicate with the mongod server on a lower level. Commands such as db.abc you run in the console are actually simple query messages referencing the db.$cmd collection as db.$cmd.findOne({ abc: 1 }) or similarly.
You thus have to either figure out how to express your mongo shell script as calls to your driver's API or access the server on a lower level.

Related

Redis error "NOSCRIPT No matching script. Please use EVAL" on Redis service restart

I'm using Redis with express-rate-limit and rate-limit-redis in my node ExpressJS server.
The problem I'm facing is that when my Redis server restarts (I know that is something shouldn't happen, but, let's just think it might happen eventually), it re-connects successfully, but then on the next request to my server, I get this error from Redis:
NOSCRIPT No matching script. Please use EVAL
I've read some info about it and I've seen I'm not the only one suffering from this problem.
But still, I haven't been able to find a solution.
What should I do to avoid this error? What options do I have?
NOSCRIPT No matching script. Please use EVAL
This means you are using EVALSHA to run a pre-loaded script. However, the script doesn't exist. Because when Redis restarts, all pre-loaded scripts will be removed.
In your case, rate-limit-redis might used some pre-loaded script, and these scripts have been removed when Redis restarts. So you need to take a look at the rate-limit-redis' doc to find a way to reload these scripts (SCRIPT LOAD).

express.js run endpoint from the command line

I have an express back end and mongodb database. I want to run a maintenance task to do some deletion of stale data in the db. The easiest and quickest way I can think to do this is use my existing code (database connection, app.js with all of the express setup, etc.) and setup a new route (ex. maintenance.js) that performs the task.
My question is how to hit the endpoint from the command line? Just running node maintenance.js won't actually cause the endpoint to execute unless I'm mistaken. My understanding is it will just make the endpoint accessible on the port the process is running on. My thought is maybe I serve the endpoint in one command line and run some command to hit it from another command line tab? Appreciate the help!
You can use mongo database connection string to connect to database using Mongo shell.
mongo <URI_string>
After the connection gets established. Use shell commands for query and deletion.
https://docs.mongodb.com/manual/reference/connection-string/

NodeJS start mongoDB server

I need to start the mongoDB server from my NodeJS application. I managed to do this before but for some reason I forgot how. I though I used a cild process but not sure anymore as I can't get anything to work at the moment.
How would I start the mongoDB server (command mongod) from withing my NodeJS app and execute some other code when the server had been started (guessing using a promise...)?
You can use child_process to run mongod from your application, but this may cause the MongoDB server to exit when your app exits. It's generally better to have the DB server running all the time.
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback

Where should a mongodb process be started for a node application

I want my application server(Hapi/Express) to start the mongodb process before proceeding with server.start(). A good way to do this is via Promises so that the mongod return code can be captured in .then and checked for start success/failure.
I posted a similar question # Nodejs exec mongodb command in Bluebird Promise, that prompted me to ask this one here.
You seem to not understand the basics of processes.
so that the mongod return code can be captured in .then
mongod will not return any code until it exits (it's called "exit code" for a reason). I assume that you want your mongodb running, so this means no code for you.
Starting a database server from the application is absolutely the wrong way to do it. Database and application should be started separately (by OS' startup manager or whatever). If you install mongodb from a package, the auto-startup should be handled for you (via installing proper init script).
Application should only know a connection string (and if it can't connect to the database at this string, show some pretty error message).

restart nodejs server programmatically

User case:
My nodejs server start with a configuration wizard that allow user to change the port and scheme. Even more, update the express routes
Question:
Is it possible to apply the such kind of configuration changes on the fly? restart the server can definitely bring all the changes online but i'm not sure how to trigger it from code.
Changing core configuration on the fly is rarely practiced. Node.js and most http frameworks do not support it neither at this point.
Modifying configuration and then restarting the server is completley valid solution and I suggest you to use it.
To restart server programatically you have to execute logics outside of the node.js, so that this process can continue once node.js process is killed. Granted you are running node.js server on Linux, the Bash script sounds like the best tool available for you.
Implementation will look something like this:
Client presses a switch somewhere on your site powered by node.js
Node.js then executes some JavaScript code which instructs your OS to execute some bash script, lets say it is script.sh
script.sh restarts node.js
Done
If any of the steps is difficult, ask about it. Though step 1 is something you are likely handling yourself already.
I know this question was asked a long time ago but since I ran into this problem I will share what I ended up doing.
For my problem I needed to restart the server since the user is allowed to change the port on their website. What I ended up doing is wrapping the whole server creation (https.createServer/server.listen) into a function called startServer(port). I would call this function at the end of the file with a default port. The user would change port by accessing endpoint /changePort?port=3000. That endpoint would call another function called restartServer(server,res,port) which would then call the startServer(port) with the new port then redirect user to that new site with the new port.
Much better than restarting the whole nodejs process.

Resources