Reputation: 12322
The following command executed with legacy mongo
shell:
mongo --quiet << EOF
show dbs
EOF
provides this output
admin 0.000GB
config 0.000GB
local 0.000GB
However, the same command with new shell (mongosh
)
$ mongosh --quiet << EOF
show dbs
EOF
provides a slightly different ouput:
test> show dbs
admin 40.00 KiB
config 36.00 KiB
local 40.00 KiB
test>
I have a set of old script using mongo
that I want to migrate to mongosh
but that slight difference in the output makes them break in some point. I wonder if I could avoid extra work adapting it configuring mongosh
to provide output in the exact same way than mongo
.
Is there some way of doing so, please?
Thanks in advance!
EDIT: the show db
above is just an example. I'm looking for a general solution that covers in general any other case. For instance, this new one:
Legacy shell:
$ echo 'db.dropDatabase()' | mongo sample-db --quiet
{ "ok" : 1 }
New shell:
$ echo 'db.dropDatabase()' | mongosh sample-db --quiet
sample-db> db.dropDatabase()
{ ok: 1, dropped: 'sample-db' }
sample-db>
In fact, just the ability of removing the prompt part that output (test>
, sample-db>
, etc.) would help a lot.
Upvotes: 0
Views: 221
Reputation: 37108
There is eval parameter for that.
Instead of
$ echo 'db.dropDatabase()' | mongosh sample-db --quiet
Do
$ mongosh sample-db --quiet --eval 'db.dropDatabase()'
It outputs exactly
{ ok: 1, dropped: 'sample-db' }
Upvotes: 1
Reputation: 59592
show dbs
is a shell internal command, thus it would be difficult.
Some commands from legacy mongo shell you can get by loading mongocompat. You can add this to your .mongoshrc.js
load('.../index.js');
To get all databases you can also query collection db.getSiblingDB('config').databases
Try
db.getSiblingDB('config').databases.find({}).forEach(x => {
print(`${x._id} ${db.getSiblingDB(x._id).stats(1000 * 1000 * 1000).storageSize}GB`)
})
It should produce the same output as show dbs
For your problem, better use
mongosh --quiet --nodb --eval 'print("Hello World")'
Hello World
instead of
echo 'print("Hello World")' | mongosh --quiet --nodb
(NoDB)> print("Hello World")
Hello World
(NoDB)>
Upvotes: 1