Reputation: 3818
Looks like there are about 5 of these types of questions, all unanswered, maybe someone who's done it will have some time to share a solution.
I've got a loopback API app running locally, and it connects fine locally to mongoDB - I've got "loopback-connector-mongodb": "^3.3.1" installed, and as mentioned - it all works fine
So here is the progression I went thru (As I'm unsure which driver is being used I tried both syntax, syntaxi? syntaxes?)
The local connection works
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "localhost",
"port": 27017,
"url": "",
"database": "test",
"user": "",
"password": "",
}
My first attempt at connecting to ALAS. This SEEMED to work, but right after the feedback it errors.
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin",
"port": 27017,
"url": "",
"database": "test",
"user": "",
"password": ""
}
Resulted in: Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer
c:\loopback-test\node_modules\mongodb\lib\replset.js:345 process.nextTick(function() { throw err; }) ^ MongoError: database names cannot contain the character '/'
I figured it didn't like '/test...', So I removed it
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
"port": 27017,
"url": "",
"database": "test",
"user": "",
"password": ""
}
Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "cluster0-shard-00-00-xxx.mongodb.net:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017",
"port": 27017,
"url": "",
"database": "test",
"user": "adminUser",
"password": "pwd"
}
Still Resulted in: Error: Cannot create data source "loopback-test": Cannot initialize connector "mongodb": double colon in host identifier
So by now Im starting to think Im using the 3.6 driver, but I want to try one more And I almost thought I had it - as it took a few seconds after the "Browse your REST API..." message, for the error to pop...but it did.
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "cluster0-shard-00-00-xxx.mongodb.net,cluster0-shard-00-01-xxx.mongodb.net,cluster0-shard-00-02-xxx.mongodb.net",
"port": 27017,
"url": "",
"database": "test",
"user": "adminUser",
"password": "pwd"
}
Still Resulted in:
Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: no mongos proxy available It will be retried for the next request.
c:\loopback-test\node_modules\mongodb\lib\mongo_client.js:421 throw err ^ MongoError: no mongos proxy available
So now IM onto the 3.6 driver syntax
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "mongodb+srv://adminUser:[email protected]",
"port": 27017,
"url": "",
"database": "test",
"user": "",
"password": ""
}
Web server listening at: http://localhost:3000 Browse your REST API at http://localhost:3000/explorer Connection fails: MongoError: failed to connect to server [cluster0-xxx.mongodb.net:27017] on first connect [MongoError: getaddrinfo ENOTFOUND cluster0-xxx.mongodb.net cluster0-xxx.mongodb.net:27017] It will be retried for the next request.
And lastly - I tried
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"host": "cluster0-xxx.mongodb.net",
"port": 27017,
"url": "",
"database": "test",
"user": "adminUser",
"password": "pwd"
}
Upvotes: 7
Views: 4505
Reputation: 1871
This one worked for me:
"db": {
"port": 27017,
"url": "mongodb+srv://user:[email protected]",
"database": "collectionName",
"name": "mongodb",
"connector": "mongodb"
},
Upvotes: 6
Reputation: 61
To clarify, the problem was caused by MongoDB Connection String URI being inserted into the host parameter instead of the url parameter.
The host parameter should only accept MongoDB server address, which can either be a hostname, IP address or UNIX domain socket.
You can find more information about the parameters on Loopback/MongoDB Connection Properties.
Upvotes: 1
Reputation: 3818
Well, hopefully I didn't waste anyones time, Since I do keep looking after I post a question... I scoured the docs for the connector - not sure why I didn't before.... But using the URL argument and passing the connection string as a URL and bypassing the other arguments, worked just fine.
"loopback-test": {
"connector": "mongodb",
"name": "loopback-test",
"url": "mongodb://adminUser:[email protected]:27017,cluster0-shard-00-01-xxx.mongodb.net:27017,cluster0-shard-00-02-xxx.mongodb.net:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin"
}
I hope that helps anyone else banging their head on the database.json arguments.
cheers - and as always - thanks for the help.
Upvotes: 3