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.
2
Answers
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
To get all databases you can also query collection
db.getSiblingDB('config').databases
Try
It should produce the same output as
show dbs
For your problem, better use
instead of
There is eval parameter for that.
Instead of
Do
It outputs exactly