小编Bir*_*ick的帖子

与Atlas上的ReplicaSet的猫鼬

我在MongoDB Atlas上有一个副本集,这是我的mongo shell连接字符串完美连接:

$ mongo "mongodb://MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE?replicaSet=MY_REPLICASET-NAME-shard-0" --ssl --username MY_USERNAME --password MY_PASSWORD --authenticationDatabase MY_ADMIN_DATABASE
Run Code Online (Sandbox Code Playgroud)

如何将其转换为在猫鼬中使用?我如何构建我的uri和options变量?

我尝试了以下但没有成功:

  // connection string using mongoose:
  var uri = 'mongodb://MY_USER:MY_PASSWORD@' +
    'MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE';

  var options = {
    replset: {
      ssl: true,
      authSource: 'MY_ADMIN_DATABASE',
      rs_name: 'MY_REPLICASET_NAME-shard-0'
    }
  };

  mongoose.connect(uri, options);
  var db = mongoose.connection;
Run Code Online (Sandbox Code Playgroud)

我尝试过包含user:和pass:on options,从uri中删除MY_USER:MY_PASSWORD @,将rs_name更改为replicaSet,每次尝试失败.似乎mongoose没有考虑authSource选项.

使用mongojs,它可以正常使用以下代码:

  // connection string using mongojs:
  var uri = 'mongodb://MY_USER:MY_PASSWORD@' +
    'MY_SERVER-shard-00-00-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-01-clv3h.mongodb.net:27017,' +
    'MY_SERVER-shard-00-02-clv3h.mongodb.net:27017/MY_DATABASE';

  var options = {
    ssl: true,
    authSource: 'MY_ADMIN_DATABASE',
    replicaSet: 'MY_REPLICASET_NAME-shard-0'
  }; …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js replicaset

6
推荐指数
1
解决办法
1万
查看次数

为什么承诺链不能按预期工作(链接任务)

使用带有'.then'的链条有什么好处,如果它不像预期的那样工作:

new Promise(function(resolve, reject) { 
    // A mock async action using setTimeout
    setTimeout(function() { resolve(10); }, 3000);
})

.then(function(num) { 
    console.log('first then: ', num); return num * 2; })

.then(function(num) { 
    setTimeout(function() { 
        console.log('second then: ', num); return num * 2; }, 500);
    })

.then(function(num) { 
    console.log('last then: ', num);
});

// RESULT!
// From the console:
// first then:  10
// last then:  undefined
// second then:  20
Run Code Online (Sandbox Code Playgroud)

我期待以下结果:

// RESULT!
// From the console:
// first then:  10 …
Run Code Online (Sandbox Code Playgroud)

javascript promise chain ecmascript-6 es6-promise

3
推荐指数
1
解决办法
211
查看次数