Mongoose连接到CosmosDB

时间:2017-07-23 15:08:39

标签: azure mongoose azure-cosmosdb

如何使用选项连接到ComosDB(Mongo API)?

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

我尝试与mongoose.connect(uri,options)建立联系,但收到500错误。

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' }

当我将uri字符串?ssl=true添加为没有选项参数的字符串时,它运行良好,否则当我将{ssl = true}作为对象作为选项参数传递时,它不起作用。

猫鼬版本为"mongoose": "~4.9.1"

问题是如何将对象的选项添加为azure CosmosDB的选项参数或字符串。

谢谢,迈克尔。

2 个答案:

答案 0 :(得分:1)

我刚刚找到解决方案:

   const qs = require('qs');


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

它将uri与选项字符串连接起来。

迈克尔。

答案 1 :(得分:0)

请尝试将"ssl": true,移至server个对象。

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