Running a local .js file on a remote mongo server

4,632 views
Skip to first unread message

Om

unread,
Aug 15, 2011, 6:48:31 PM8/15/11
to mongodb-user
Hi,

I tried to run

mongo --host 10.33.1.30 --port 27017 --eval om.js
MongoDB shell version: 1.8.2
connecting to: 10.33.1.30:27017/test
Mon Aug 15 22:44:41 ReferenceError: om is not defined (shell eval):1

Basically I want to execute the contents of the local om.js file on
the remote mongo server located at 10.33.1.30. How can this be done?

Scott Hernandez

unread,
Aug 15, 2011, 6:52:00 PM8/15/11
to mongod...@googlegroups.com
Copy the file over, and run mongo on the server with it.

There is no support for this directly in the server/client. You could
also use db.eval to run it on the server but the first method is
preferable IMHO.

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Om

unread,
Aug 15, 2011, 6:57:13 PM8/15/11
to mongodb-user
So it can't be done remotely then i.e. what if I can't copy the file
over? mongo doesn't allow a remote script execution?

Scott Hernandez

unread,
Aug 15, 2011, 6:59:13 PM8/15/11
to mongod...@googlegroups.com

Om

unread,
Aug 15, 2011, 7:04:47 PM8/15/11
to mongodb-user
Scott,

I read that document already. However, I couldn't find out if mongo
supports remote query execution (like the one I'm looking for) i.e.
can I remotely connect to a mongo server and execute a js query on it?
Regards

Scott Hernandez

unread,
Aug 15, 2011, 7:08:38 PM8/15/11
to mongod...@googlegroups.com
You can use db.eval(...), or you can copy the file to the server and
use ssh (or some other tool) to remotely execute the mongo shell.

The mongo javascript shell (mongo) is a client just like any other
program. It uses a driver to communicate to the server using the
binary wire protocol.

Om

unread,
Aug 15, 2011, 7:37:33 PM8/15/11
to mongodb-user
Scott .. essentially what I want to do is to add a node to a replica
set remotely (i.e. without executing rs.add() on the Primary
physically). I was thinking maybe I can execute rs.add() command on
that machine remotely (from a secondary). However, I read on the page
that running it this way may cause write lock issues. Is there a way
to add a secondary node remotely to a replica set? My thinking was to
write a java script at the secondary which will add the secondary to
the rs. Then I'll execute the script on the primary remotely. Can this
work?

Scott Hernandez

unread,
Aug 15, 2011, 8:14:50 PM8/15/11
to mongod...@googlegroups.com
All you have to do is use the client (mongo) to connect to the primary
and run rs.add(...). You don't need to be on the same host.

$mongo primary.server
....
> rs.add("newNode:port")

Om

unread,
Aug 15, 2011, 8:25:09 PM8/15/11
to mongodb-user
Thanks. I already did that :D. But thanks a lot for your time and
help :D

On Aug 15, 5:14 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> All you have to do is use the client (mongo) to connect to the primary
> and run rs.add(...). You don't need to be on the same host.
>
> $mongo primary.server
> ....
>
>
>
>
>
>
>
> > rs.add("newNode:port")

Om

unread,
Aug 15, 2011, 8:34:34 PM8/15/11
to mongodb-user
Actually I had the right idea .. just that my syntax was wrong.
Instead of

mongo --host 10.33.1.30 --port 27017 --eval om.js

if you try,

mongo host:port om.js

it works fine. I tried the previous syntax based on --help reply.
Seems like I got the syntax wrong. Thanks for the correction Scott :D
Reply all
Reply to author
Forward
0 new messages