I wanted to connect to a Realm-enabled database with mongo://
url in typescript (NodeJS). I wrote like:
const apiKey = process.env.REALM_API_KEY;
const appId = process.env.REALM_APP_ID;
const mongoUrl = `mongodb://_:${apiKey}@realm.mongodb.com:27020/?authMechanism=PLAIN&authSource=%24external&appName=${appId}:mongodb-atlas:api-key&ssl=true`;
console.log(mongoUrl);
const client = await MongoClient.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
This produces an error:
MongoServerSelectionError: failed to handle command request "ismaster": error processing client metadata: expected app name to be composed of: <appID>:<svc>:<authProvider>, got [object Object]
I understand there is remoteMongoClient
to do the similar thing, but I want to know whether mongodb://
url can be used as well.
What I did:
- checked ‘MongoDB Connection String’ is enabled in Realm UI
Linked Data Sources
section - tried both with and without
?ssl=true
(the error message changes though) - checked
console.log(mongoUrl)
to see the url is properly provided - tried the same url in
mongo
command (mongo "mongodb://......")
and see it works in the shell - put the same url in Compass to connect, and got the same error
Why I wanted:
- I wanted to try out 3rd party ORM libraries while utilize Realm’s great user/role system. Those libraries often require the
mongodb://
url to initialize connection.