0

For old mongo client i've using .mongorc.js script, which automaticly authenticate and connect to specified in parameter database:

olddb=db;
db = connect("127.0.0.1:27017/admin?replicaSet=replSet");
db.auth("root", "password" );
db = db.getSiblingDB(olddb);

How to do the same using mongosh? Simply copy .mongorc.js into .mongoshrc.js provide error:

Error while running ~/.mongoshrc.js:
MongoServerError: Authentication failed.

I can authenticate using something like:

olddb=db;
db = connect("mongodb://root:password@127.0.0.1:27017/admin?replicaSet=replSet");

but - try to switch into database specified as parameter show an error:

# mongosh test
Current Mongosh Log ID: XXX
Connecting to:      mongodb://127.0.0.1:27017/test?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:      5.0.2
Using Mongosh:      1.0.5

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

replSet [primary] admin> olddb
test
replsSet [primary] admin> db.getSiblingDB(olddb)
MongoshInvalidInputError: [COMMON-10001] Argument at position 0 must be of type string, got object instead (Database.getSiblingDB)
replSet [primary] admin> db.getSiblingDB("test")
test
replSet [primary] admin> 
undefine
  • 142
  • 1
  • 15
  • Try connection string `"mongodb://root:password@127.0.0.1:27017/olddb?replicaSet=replSet&authSource=admin"`. See https://stackoverflow.com/questions/63754742/authentication-failure-while-trying-to-save-to-mongodb/63755470#63755470 - and personally I think the new `mongosh` is still far away from being usable. – Wernfried Domscheit Aug 23 '21 at 17:42
  • you could use `db.getSiblingDB(olddb.getName())`. It seems the db.toString() method does not return a string. – barrypicker Aug 23 '21 at 18:24

0 Answers0