Search code examples
dockerazure-cosmosdbazure-cosmosdb-mongoapi

MongoError: database name must be string


My node backend is connecting to Cosmos Db using the Mongo API. Connecting via mongoose via the below steps yields the error

MongoError: database name must be a string

when running in docker. It runs just fine when using nodemon, i.e. outside of docker container.

( < any > Mongoose).Promise = global.Promise;
const options: ConnectionOptions = {useNewUrlParser: true, useFindAndModify: false};
const mongoURI: string = process.env.COSMOS_DB_CONN_STR;
connect(mongoURI, options)
.then(() => console.log(`Mongo DB: connected`))
.catch(err => console.log(`Could not connect. Error: ${err}`));

process.env.COSMOS_DB_CONN_STR is of the format

mongodb://<USER_NAME>:<KEY>@<DB_INSTANCE_NAME>.documents.azure.com:<PORT>/?ssl=true&replicaSet=globaldb

Any thoughts why this is happening? The test environment of this service uses a separate cosmos db which works fine, inside and outsode of docker.


Solution

  • The connection string was malformed. I was missing the db name, duh...

    mongodb://<USER_NAME>:<KEY>@<INSTANCE_NAME>.documents.azure.com:<PORT>/<DBNAME_WAS_MISSING_HERE>?ssl=true&replicaSet=globaldb