I have a replica set on Atlas MongoDB, and this is my mongo shell connection string that 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 for use in mongoose? How can I create 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 tried to enable user: and pass: by parameters, removing MY_USER: MY_PASSWORD @ from uri, change rs_name to replicaSet, every failed attempt. The mongoose doesn't seem to be considering an authSource option.
Using 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 ODM is in my project.
How can I create my uri and options variable using mongoose?