2

I have a new sandbox cluster on MongoDB Atlas that I am connecting to with mongoose on a Node.js server. This is the code I'm using to connect:

const mongoDbUrl =
  `mongodb+srv://${username}:${password}@mydb-sandbox-12345.mongodb.net/testdb`

mongoose.connect(mongoDbUrl)
const connection = mongoose.connection

connection.on('connected', () => {
  console.log('Connected to mongodb')
})

In the Atlas dashboard I have a readWriteAnyDatabase user that I am authenticating with. Authentication works as expected. I am able to connect to the database and no error is thrown on the connection. I can confirm this by removing a character in the password - authentication fails and I'm unable to connect.

The problem is when I try to insert documents.

const UserModel = require('./models/User')

UserModel.create({
  name: 'Hello Atlas'
})

I get the following error:

MongoError: not authorized on admin to execute command {
insert: "users",
  documents: [
    [{
      name Hello Atlas
    } {
      _id ObjectIdHex("5aa17933d72d25730a340611")
    } {
      __v 0
    }]
  ],
  ordered: false
}

As far as I know the user I'm authenticating with should have permission to read and write on the database I'm connecting to. The other part I don't understand is that the error shows that it's trying to write to admin even though my url is connecting to testdb.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
  • One additional thing worth noting - I tried writing the same thing using the vanilla MongoDB drivers and it works. I connect to the same exact URL with the same auth credentials, and I am able to add a new user object to testdb. I'm wondering if this is something up with my mongoose configuration. – Colin Hemphill Mar 08 '18 at 18:15

5 Answers5

7

Not sure if you have seen this post, but it could be because you are on a free cluster? Hope this helps.

UPDATE

I looked into the problem further and reproduced it on my own. I got the same error. However, I noticed that at one point Atlas provided me with a choice of connection strings. I went back to that page and chose I am using driver 3.4 or earlier.

The connection string looks like this:

const mongoDbUrl = `mongodb://${username}:${password}@cluster0-shard-00-00-1wntz.mongodb.net:27017,cluster0-shard-00-01-1wntz.mongodb.net:27017,cluster0-shard-00-02-1wntz.mongodb.net:27017/testdb?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin`;

It worked with that connection string.

It looks like the free version of MongoDB Atlas launches with v3.4

thomann061
  • 629
  • 3
  • 11
  • hmm... I don't see any unsupported commands or limitations here that would apply to my simple write operation, but that is possible. Not sure that would explain why it works through the vanilla driver but not through Mongoose, unless Mongoose is doing something weird under the hood. – Colin Hemphill Mar 08 '18 at 22:05
  • 1
    Good catch on the driver version. I think I had tried the 3.4 connection string at some point, but I must have used it incorrectly before. Vanilla drivers worked with the 3.6 connection string, but mongoose doesn't like it. Seems to work though - thanks! – Colin Hemphill Mar 09 '18 at 00:45
  • Great answer, you saved me a lot of time. – Huiyang Shan Aug 06 '19 at 03:33
3

If you are using free cluster. change 'admin' to 'test' in the path: mongodb+srv://username:password@cluster0-yauj8.mongodb.net/test?retryWrites=true&w=majority

This worked for me

1

Going off @rithesh-mys 's answer. Replacing it with "test" is specifying the db. So you have to change it to the db name that you would use.

danthegoodman
  • 501
  • 4
  • 10
0

I had the same problem. I was trying a lot of connection strings, but the one for olders mongo shell (3.4 or earlier) worked for me.

mongodb://my_username:my_password@cluster0-shard-00-00.osobw.mongodb.net:27017,cluster0-shard-00-01.osobw.mongodb.net:27017,cluster0-shard-00-02.osobw.mongodb.net:27017/my_database?ssl=true&replicaSet=atlas-xw3rfy-shard-0&authSource=admin

I think that newer versions of connection strings don't work with mongoose, at least with free clusters.

0

Make sure the user you have created has the write and read privileges.

Gesy Darati
  • 113
  • 1
  • 6