Passing command line parameters to mongo while executing some js file

1,343 views
Skip to first unread message

hdev ml

unread,
Nov 23, 2010, 2:34:12 PM11/23/10
to mongod...@googlegroups.com
Hi,

Bigger picture is
Write a shell script to
1. Process some data outside mongodb
2. import that into mongodb into a temporary collection
3. copy that data into an old collection.

I want to write a common function for #3. So I wrote a function like

var copyCollection = function(from, to) {

        var cursor = db[from].find();
        while(cursor.hasNext())
        {
                db[to].save(cursor.next());
        }
}


And from the script I will execute it as

./mongo some.js

But how do I pass the "from" and "to" parameters from shell script? Is there any tweak to do that?

Appreciate the help
Thanks
Harshad

Dwight Merriman

unread,
Nov 23, 2010, 6:51:37 PM11/23/10
to mongod...@googlegroups.com
use --eval on the mongo command line

p.s. insert() might be a little faster than save() - not sure

--
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.

hdev ml

unread,
Nov 23, 2010, 6:56:09 PM11/23/10
to mongod...@googlegroups.com
Thanks Dwight. Will try that.

robo -

unread,
Nov 24, 2010, 10:17:51 AM11/24/10
to mongod...@googlegroups.com
I've been trying to do the same thing. Can someone please provide a
simple example of how to use the --eval to pass arguments on the
command line to a javascript file being passed in?

Thanks,

robo

Eliot Horowitz

unread,
Nov 24, 2010, 12:16:13 PM11/24/10
to mongod...@googlegroups.com
mongo --eval "db.foo.insert( { x : 1 } )"

robo -

unread,
Nov 24, 2010, 3:02:52 PM11/24/10
to mongod...@googlegroups.com
Thanks a million. That gives me a starting point from which I can expand upon.

robo

Reply all
Reply to author
Forward
0 new messages