MongoDB commands in linux console with --eval (print

966 views Asked by At

I try run command mongo in linux console without entering into mongo shell. To do it:

[root@router-mongos ~]# mongo --eval " printjson(show databases)"

But not run, this is the output:

MongoDB shell version: 2.6.10 connecting to: test 2015-06-10T18:33:39.834+0200 SyntaxError: Unexpected identifier

Though if you are uses:

[root@router-mongos ~]# mongo maria --eval " printjson (db.stats())" o mongo --eval " printjson(db.adminCommand('listDatabases'))"

Yes, the output is the same if you run it in shell.

Has anyone ever used this? can we help me? Thanks.

1

There are 1 answers

0
Sylvain Leroux On

From the Mongo Shell documentation:

You cannot use any shell helper (e.g. use <dbname>, show dbs, etc.) inside the JavaScript file because they are not valid JavaScript.

Following that, there is a table showing the JavaScript equivalents of the various shell helpers. From that, show dbs and show databases should be replaced by db.adminCommand('listDatabases') in your Mongo shell scripts.