Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing JavaScript file in MongoDB

I would like to know how to execute a JavaScript file in MongoDB.

This is the simple piece of code present in my JS file:

function loadNames() {
    print("name");
}

From the command prompt I tried to execute the file like this

mongo test.js

but it shows the error:

unexpected identifier

Can anyone explain to me where I'm going wrong?

like image 605
user1585111 Avatar asked Mar 13 '26 07:03

user1585111


1 Answers

I usually run my test queries like this, and it works with your sample code too.

mongo < test.js
like image 161
David Filipidisz Avatar answered Mar 14 '26 21:03

David Filipidisz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!