Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose with ReplicaSet on Atlas

I have a replica set on MongoDB Atlas and this is my mongo shell connection string which connects perfectly:

$ 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

How Can I convert it to use in mongoose? How Can I build my uri and options variable?

I tried the following without success:

  // 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;

I've tried including user: and pass: on options, removing MY_USER:MY_PASSWORD@ from uri, change rs_name to replicaSet, every unsuccessful attempt. It seems that mongoose is not considering the authSource option.

Using the mongojs, it works fine with the following code:

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

  var db = mongojs(uri,'', options);

But, I need to use mongoose because the ODM in my project.

How can I build my uri and options variable using mongoose?

like image 649
Biruel Rick Avatar asked Dec 18 '16 21:12

Biruel Rick


1 Answers

ON MONGODB 3.4.x

I resolved this issue putting the 'options' value directly in 'uri' string, according to documentation (http://mongoosejs.com/docs/connections.html) on 'Replica Set Connections' section.

// 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' +
  'ssl=true&replicaSet=MY_REPLICASET_NAME-shard-0&authSource=MY_ADMIN_DATABASE';

mongoose.connect(uri);
var db = mongoose.connection;

Now, it is working fine!

NOTICE WITH MONGODB 3.6

On MongoDB Atlas using the version 3.6.x, the connection string changed to use a DNS server making the link shorter.

mongodb+srv://MY_USER:MY_PASSWORD@MY_SERVER.mongodb.net/MY_DATABASE

...if you use this connection string in your application, this will connect with success but it will be able to read and write only with atlas users with higher privilegies access (atlasAdmin, readWriteAnyDatabase...).

To you work with an specific user with privilege only to readWrite your database, you will need to keep the same connection string used in MongoDB 3.4 because the mongoose not recognized the DNS option (mongodb+srv).

P.S. all the new resources from MongoDB 3.6.x will continue working normally!

like image 57
Biruel Rick Avatar answered Nov 07 '22 20:11

Biruel Rick