I am trying to connect to my mongoDB atlas cluster but get an authentication fail.
I am able to connect from a client like Studio 3T and from the Mongo shell.
Here's my connection URI:
var conn = mongoose.connect("mongodb://<user>:<password>#xxx-shard-00-00-kqmqb.mongodb.net:27017,xxx-shard-00-01-kqmqb.mongodb.net:27017,xxx-shard-00-02-kqmqb.mongodb.net:27017/myDB?ssl=true&replicaSet=xxxCluster-shard-0&authSource=admin")
I copied this from the atlas console.
I'm using Mongoose 4.9.7 which uses MongoDB 2.2.26 so I'm using the latest versions of these modules.
The error I get is the following:
MongoError: authentication fail
Any idea what this could be?
Found the solution. My password has special characters so I have to encode it properly. Changed the implementation to
var f = require('util').format;
var user = encodeURIComponent('user');
var password = encodeURIComponent('p#ssw0rdWithSpecialCharacter');
var url = f("mongodb://%s:%s#xxx-shard-00-00-kqmqb.mongodb.net:27017,xxx-shard-00-01-kqmqb.mongodb.net:27017,xxx-shard-00-02-kqmqb.mongodb.net:27017/myDB?ssl=true&replicaSet=xxx-shard-0&authSource=admin",user,password);
var conn = mongoose.connect(url);
Related
In my nodejs application I have connection created to mongodb using the MongoDB driver.
const client = await mongodb.MongoClient.connect(connectionString)
This works fine. But now I am trying to reuse the same connection with Mongoose, using the setClient() function as mentioned in the docs:
https://mongoosejs.com/docs/api/connection.html#connection_Connection-setClient
const conn = mongoose.createConnection().setClient(client);
conn.readyState; // is 1 which indicates CONNECTED
But when I try to run query using Mongoose models like:
const data = await MyModel.find()
I get a buffering timed out after 10000ms error
Is this the correct way of using a existing mongo connection?
Alternative approaches welcomed as well
I'm using node.Js, expressjs mongodb and Atlas
var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
the above method is not working for me.
by using atlas database. you are given three nodes with three different host/Url now the problem here is that when I try to connect to mongodb.server it only ask for one host name (or its allowed to add many but I just don't know how)
my question would be, how can I make this work? like how can I join together 3 different Url and let 1 port let it in. and connect to database server
you are given three nodes with three different host/Url now the problem here is that when I try to connect to mongodb.server it only ask for one host name
MongoDB Atlas provides you with a MongoDB Connection URI. The connection string should contain host(s) information.
You can also see a snippet example of MongoDB Node.js connecting to MongoDB Atlas on the manual MongoDB Atlas: Node.js Driver Example
MongoClientURI uri = new MongoClientURI(
"mongodb+srv://user:password#cluster0.mongodb.net/");
MongoClient mongoClient = new MongoClient(uri);
MongoDatabase database = mongoClient.getDatabase("databaseName");
MongoDB Version 3.4 and earlier:
var MongoClient = require('mongodb').MongoClient;
var uri = "mongodb://user:password#mycluster0-shard-00-00.mongodb.net:27017,mycluster0-shard-00-01.mongodb.net:27017,mycluster0-shard-00-02.mongodb.net:27017/admin?ssl=true&replicaSet=Mycluster0-shard-0&authSource=admin";
MongoClient.connect(uri, function(err, db) {
db.close();
});
For other drivers, please see MongoDB Atlas: Connect via Driver
I am working on a project that requires a dedicated database per registered user. I prefer working with MongoDB so I'm using that for the same (Am I Right?). The app uses a REST API as the backend (written in Node Express) and an AngularJS App. So, what I think of doing is whenever a user makes a request to some API endpoint say, a GET request to api/user/mydata, I would create a connection to his particular database, fetch the required data, close the connection and return the fetched data as the response. Is this approach correct? Also, I'm using Mongoose as the ODM and PassportJS for user Authentication. Moreover, users of my app are mutually exclusive. There is no data connection between a user with any other registered user.
There's a way to do that but only without using Mongoose. You would have to create a root connection to your MongoDB server (mind it, not to a particular database on that server) using the mongodb node module and then you can switch between the database as per your query requirement without creating a new connection per database as shown below:
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
// URL to the root of MongoDB Server and not a particular db
const url = 'mongodb://localhost:27017';
// Database Names
const dbName1 = 'myproject1';
const dbName2 = 'myproject2';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, client) {
assert.equal(null, err);
console.log("Connected successfully to server");
const db1 = client.db(dbName1);
const db2 = client.db(dbName2);
client.close();
});
You can't do this through mongoose, as mongoose and its models require connection to be made to a particular database and not to just the root db server. Anyways, I didn't want to give up mongoose for my own project so I just had to resort to initializing the db connection and its models per HTTP request by the user and closing the connection upon response.
I get this error when trying to connect with MongoClient.connect, but only when I'm offline. As soon as I'm online, with no change in code and not even restarting mongoDB, my app connects every time.
AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect
My server.js looks like this:
// server.js
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var operations = require('./operations');
// Connection URL
var url = 'mongodb://localhost:27017/myApp';
// Use connect method to connect to the Server
MongoClient.connect(url, function (err, db) {
assert.equal(null, err);
...
If I just use > mongo in the terminal, it connects with no problem even when offline. Did I miss something in the documentation?
Sorry! Only after I posted did Mongoose can't connect without internet appear in the related questions sidebar - not that I'm using Mongoose, but the answer exactly solved my problem also.
I have two node.js applications, both of them should connect to the same mongodb database. I should insert document in one application and retrieve it from the other application. I added the same connection of the database to both application. My database is on localhost.
var MongoClient = require('mongodb').MongoClient;
var MONGO_HOSTS = {
undefined: 'localhost:27017',
'development': 'localhost:27017',
'test': 'localhost:27017',
'staging': process.env.MONGO_HOST,
'production': process.env.MONGO_HOST
};
// Connection URL
var url = 'mongodb://' + (process.env.MONGO_HOST || MONGO_HOSTS[process.env.NODE_ENV]) + '/test' + process.env.NODE_ENV;
Then connect to the database. The problem is that when I insert in one application the other application don't find it. Is this valid way of connection or is there other way.
Communicating via database is not the best pattern. But if you would like to do that, make sure you're trying to read data from DB after write was successful. Try also finding that document using first DB to make sure that your 'find' query is working.