Execute a Java Script file

304 views
Skip to first unread message

Tim

unread,
Sep 6, 2012, 3:39:07 PM9/6/12
to mongod...@googlegroups.com
Hello,
I developed a Java Script function, that inserts data and measures the time. (Tried it with and without the "function()")

function f() {

var doc= '';
var start = new Date().getTime();

for (i=0; i<1000000; i++)
{
doc = { a: '13', b:i} 
db.mycol.insert(doc);
}
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time);
}



I tried to execute via the mongos shell like


If a line contains open '(' or '{' characters, the shell will request more input before evaluating:

> function f() {
... x = 1;
... }
>
 
from
http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-SomeNotesonDatatypesintheShell

but it failed.

From http://www.mongodb.org/display/DOCS/Scripting+the+shell
I read:

./mongo server:27017/dbname --quiet my_commands.js

I'm not quite sure if I missed something, but is there a folder where I have to store the .js file?
Or how do you execute a .js file?

Stephen Lee

unread,
Sep 6, 2012, 4:10:52 PM9/6/12
to mongod...@googlegroups.com
Could you post the error (w/o --quiet)?  BTW, alert() is not a valid function using the MongoDB Javascript shell.  Also, you should be able to run the Javascript w/o wrapping your code in a function f() {...}.  If you do wrap it in function f() {...}, then call f(); at the end of your .js file.

Tim

unread,
Sep 6, 2012, 4:37:59 PM9/6/12
to mongod...@googlegroups.com
Hi,

I removed the "time measure part" and executed the code
mongo hostip:port/mydb --quiet /data/insert.js
After a few seconds the codee seems to be executed, but my collection is empty (db.mycol.count();)

I also tried to put all the code into the mongos shell (with the time measure part)
the errors:

mongos> for (i=0; i<amount; i++)
Thu Sep  6 21:20:43 SyntaxError: syntax error (shell):1



mongos> var time = end - start;

                               alert('Execution time: ' + time);
Thu Sep  6 21:20:44 ReferenceError: start is not defined (shell):1



Thu Sep  6 21:44:25 ReferenceError: i is not defined (shell):2

Tim

unread,
Sep 6, 2012, 5:17:59 PM9/6/12
to mongod...@googlegroups.com
I executed:

mongo hostip:port/mydb --quiet /data/insert.js
and it worked without the function and the time measure part.
Do you have an idea how I could measure the time that is needed to execute that .js?

Is it possible to run a .js file from the mongos shell?



Am Donnerstag, 6. September 2012 21:39:07 UTC+2 schrieb Tim:

Stephen Lee

unread,
Sep 7, 2012, 11:32:38 AM9/7/12
to mongod...@googlegroups.com
When you paste the Javascript into the mongo console, the interpretter throws a syntax error on the for loop line because the opening { is on a different line.  If you run it as a script, the parser handles the Javascript correctly.

In general, I can't reproduce your error.  When you did a db.mycol.count(), did you first "use mydb" to ensure that you're referencing mydb.mycol?

Tim

unread,
Sep 8, 2012, 4:44:17 AM9/8/12
to mongod...@googlegroups.com
Hi,

I think the error occours by using the "time measure part". Without that part it works.
I use "time" in front of the command
time mongo hostip:port/mydb --quiet myscript.js
and
var a=Date.now();db.mycol.count();Date.now() - a
for e.g. the count function

thanks

Stephen Steneker

unread,
Sep 8, 2012, 4:57:33 AM9/8/12
to mongod...@googlegroups.com
I think the error occours by using the "time measure part". Without that part it works.
I use "time" in front of the command

Hi Tim,

I believe the problem is that "alert()" is not a function in the mongo shell .. you should instead use print(), eg:

      print('Execution time: ' + time);

Also, if you are going to wrap your code in a function f() you will need to call it at the end of your .js file (otherwise you have defined the function without running it).

Then you can execute this JS from the command line using:
  mongo hostip:port/mydb my_commands.js

You should be able to test by pasting the contents of your .js file into the mongo shell.

Cheers,
Stephen

Tim

unread,
Sep 8, 2012, 5:47:49 PM9/8/12
to mongod...@googlegroups.com
Hi,


 print('Execution time: ' + time);

works, thanks ;)


Am Donnerstag, 6. September 2012 21:39:07 UTC+2 schrieb Tim:
Reply all
Reply to author
Forward
0 new messages