Multiples commands in one line

11,217 views
Skip to first unread message

Fernando Cappi

unread,
May 4, 2013, 4:46:35 PM5/4/13
to mongod...@googlegroups.com
Hi,

How can i join two or more commands in one line, on the mongo shell?

Example: rs.initiate();rs.add("host:port")

Thanks,

Fernando

Asya Kamsky

unread,
May 4, 2013, 7:58:50 PM5/4/13
to mongod...@googlegroups.com
Just like that.   Semicolon will separate two commands.  They will be run in order that you enter them in.

Fernando Cappi

unread,
May 4, 2013, 8:50:15 PM5/4/13
to mongod...@googlegroups.com
The following doesn't work:

rs0:PRIMARY> show dbs; show dbs;
Sat May  4 21:49:04.707 JavaScript execution failed: don't know how to show [dbs;] at src/mongo/shell/utils.js:L847

Jorge Puente Sarrín

unread,
May 4, 2013, 8:58:30 PM5/4/13
to mongod...@googlegroups.com
Hi Fernando,

show dbs is a simple helper:

You should to use: db.getMongo().getDBs();



--
--
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
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Jorge Puente Sarrín.

Asya Kamsky

unread,
May 4, 2013, 10:23:57 PM5/4/13
to mongod...@googlegroups.com
You need a space after dbs like this:

  >  show dbs ; show dbs

On regular commands (as opposed to helpers) you won't need it.

What exactly are you trying to do that can't be done on multiple lines?

Asya

Fernando Cappi

unread,
May 5, 2013, 9:27:10 AM5/5/13
to mongod...@googlegroups.com
i'm trying to initializate replicaset and add 2 members to it:

mongo --eval "rs.initiate();rs.add('host1:port1');rs.add('host2:port2')"

The problem is that only the first command (rs.initiate()) is executed

Asya Kamsky

unread,
May 5, 2013, 12:53:38 PM5/5/13
to mongod...@googlegroups.com
Are you sure about this?   It would be better if you explicitly print out the returned status of each command (it's returned as json so you have to use printjson rather than print.

$ mongo --quiet --eval "printjson(rs.initiate());printjson(rs.add('host1:port1'));printjson(rs.add('host2:port2'))"
{ "ok" : 0, "errmsg" : "server is not running with --replSet" }
{ "ok" : 0, "errmsg" : "not running with --replSet" }
{ "ok" : 0, "errmsg" : "not running with --replSet" }

Obviously I got errors from all because I'm not running the mongod I sent these to with replSet option :)

Rather than sending all the commands as a single line to eval why not put them into a script file (.js file)?

initRepl.js
var statusInit = rs.initiate();
var statusAdd1 = rs.add('host1:port1');
var statusAdd2 = rs.add('host2:port2');

if (statusInit.ok==0 || statusAdd1.ok==0 || statusAdd2.ok==0) {
   print("Not everything was okay! \n" + tojson(statusInit) + "\n" + tojson(statusAdd1) + "\n" + tojson(statusAdd2));
}

Now run this with 
$ mongo --quiet initRepl.js
Not everything was okay!
{ "ok" : 0, "errmsg" : "server is not running with --replSet" }
{ "ok" : 0, "errmsg" : "not running with --replSet" }
{ "ok" : 0, "errmsg" : "not running with --replSet" }

Makes it a lot more manageable and you can add a lot more functionality and error checking, etc.  Plus it's in a file so you can rerun it in the future whereas command history may disappear.

Asya

Fernando Cappi

unread,
May 5, 2013, 4:06:21 PM5/5/13
to mongod...@googlegroups.com
Thanks Asya, it works!! I will use the .js file

Fernando
Reply all
Reply to author
Forward
0 new messages