0

I have complex js file that do some heavy calculation and save result to collection. This written in js because i wan't to avid data transmission.

currently i pass script to mongo shell in this way : $mongodb < path_to_script

The script consists of several functions?

Is it possible to execute it from PHP? I saw there is 'nolock' parameter that can be pass to evel method, is it possible to use it when executing from shell or from PHP?

What is consider more safe , using php execute wrapper or executing script from shell ?

2 Answers 2

1

When you run a .js file in the mongo shell, it's not running the javascript on the server; it's running it in the shell. Are you using server-side javascript features like db.eval and map/reduce in the mongo shell script?

Either way, I'd suggest forgoing the shell script and server-side javascript functions and either using the aggregation framework for server-side processing or implementing the logic in PHP application code. Server-side javascript has serious performance and security limitations and it's best to avoid using it when possible.

Sign up to request clarification or add additional context in comments.

1 Comment

The operation is scheduled to run every hour (cron). I couldn't implement it with aggregation framework / map-reduce because i missed capabilities. Why it is not safe to run js ?
0

You can read the js file and execute it in PHP using execute which is a wrapper to the eval command. The nolock option can be added in the arguments.

$code = file_get_contents('filenam.js');
$db->execute($code, array('nolock' => true));

The php execute wrapper should be safer and better solution. If you execute js using shell in PHP, you will need to give permission to php to execute shell script using shell_exec(), which is impossible if you use hosting service and don't have admin right on the host.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.