mongosh parse mongo url offline
In scripting easiers and most robust way to pass mongodb connection info using url string. Sometimes though there is need to parse part of it, like database name or specific query params. In NodeJS whathg URL can be used for this, additionally validating url. For mongsh only way at the moment is to perform actual connecion to running mongodb:
mongosh "mongodb://127.0.0.1:27017/staging?serverSelectionTimeoutMS=15000&readPreference=secondaryPreferred" --quiet -eval "print(db)"
staging_db
Would be nice if mongosh can be used to parse mongodb URL and output some part of it wihtout need for actual connection. This use case if for bash scripting, currently working on combination of bash and mongosh scripts for my need and it works out pretty effective.