Michael Horojanski
Michael Horojanski

Reputation: 4701

Mongoose connect to CosmosDB

How to connect to ComosDB(Mongo API) with options?

"options": {
  "ssl": true,
  "server": {
    "socketOptions": {
      "keepAlive": 300000,
      "connectTimeoutMS": 30000
    }
  },
  "replset": {
    "socketOptions": {
      "keepAlive": 300000,
      "connectTimeoutMS": 30000
    }
  }
}

I tried to connect with mongoose.connect(uri,options) but I received 500 error.

err:  { MongoError: connection 0 to xName.documents.azure.com:port timed out
    at Function.MongoError.create (/home/mic3ael/src/prizmacloud/node_modules/mongodb-core/lib/error.js:29:11)
    at Socket.<anonymous> (/home/mic3ael/src/prizmacloud/node_modules/mongodb-core/lib/connection/connection.js:188:20)
    at Object.onceWrapper (events.js:314:30)
    at emitNone (events.js:105:13)
    at Socket.emit (events.js:207:7)
    at Socket._onTimeout (net.js:401:8)
    at ontimeout (timers.js:488:11)
    at tryOnTimeout (timers.js:323:5)
    at Timer.listOnTimeout (timers.js:283:5)
  name: 'MongoError',
  message: 'connection 0 to xName.documents.azure.com:port timed out' }

when I added to uri string ?ssl=true as a string without options param, it worked well otherwise when I passed {ssl=true} as an object as options param, it didn't work.

mongoose version is "mongoose": "~4.9.1"

The question is how to add options an object as options param for azure CosmosDB or as a string.

Thanks, Michael.

Upvotes: 0

Views: 847

Answers (2)

Michael Horojanski
Michael Horojanski

Reputation: 4701

I have just found the solution:

   const qs = require('qs');


  mongoose.connect(`mongodb://${config.username}:${config.password}@${config.host}:${config.port}/${config.database}?${qs.stringify(config.options)}`)

It connects uri with options string.

Michael.

Upvotes: 1

Aaron Chen
Aaron Chen

Reputation: 9940

Please try to move "ssl": true, to server object.

"options": {  
  "server": {
    "ssl": true,
    "socketOptions": {
      "keepAlive": 300000,
      "connectTimeoutMS": 30000
    }
  },
  "replset": {
    "socketOptions": {
      "keepAlive": 300000,
      "connectTimeoutMS": 30000
    }
  }
}

Upvotes: 0

Related Questions