1
votes

I have created a cluster in MongoDB Atlas and can't seem to be able to write data to it.

const uri = "mongodb+srv://myUser:myPass@myDB-4myav.mongodb.net/portfolio";

I have this as my uri to connect to, and every time I try to write to the database, I get this error:

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

I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:

Error: couldn't add user: not authorized on admin to execute command

So basically I don't have a user that can write to my database.

I've also tried making a user with every role possible on the MongoDB Atlas website (for my cluster of course) and then connecting through the mongo shell with it, but that failed as well.

To summarize: I've made a new cluster on MongoDB Atlas. How do I write data to it?

Thanks in advance, feel free to point out if I'm missing something simple and stupid.

2
You need to manage users via Atlas (docs.atlas.mongodb.com/security-add-mongodb-users) and create a user account with the "Read and write to any database" role (or more refined privileges with the Advanced Options).Stennie
I've done that. Even better, I've made an account with every possible role as well and I still get denied.danielv
Ah! The error indicates your code is trying to insert documents into the admin database, which is a system database reserved for user and role information. In your connection string you intended to use portfolio, but the srv connection string does not support specifying a database so you need to change to this database after connecting. See github.com/Automattic/mongoose/issues/6106 for a workaround.Stennie

2 Answers

5
votes

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

This error indicates your code is trying to insert documents into the admin database, which is a system database reserved for user and role information.

In your connection string you intended to use the portfolio database. However, the srv connection string format does not support specifying a database so your option was ignored and the default database of admin was used. You need to select the portfolio database after connecting.

This was also reported in the Mongoose issue tracker (GitHub issue #6106) where a user posted a workaround you could adapt:

mongoose.connection.on('connected', function() {
    // Hack the database back to the right one, because when using mongodb+srv as protocol.
    if (mongoose.connection.client.s.url.startsWith('mongodb+srv')) {
        mongoose.connection.db = mongoose.connection.client.db('portfolio');
    }
    console.log('Connection to MongoDB established.')
});
mongoose.connect(...);

I have read that I need to create an admin user with the role of "root" but when I connect to my database using the mongo shell and try creating it, I get this:

You need to manage users via MongoDB Atlas rather than the mongo shell. You can create a user account with the "Read and write to any database" role (or more refined privileges using Advanced Options).

3
votes

You need to create a user with root role from Mongo Shell,

use admin
db.createUser(
{
   user: "admin",
   pwd: "password",
   roles: [ { role: "root", db: "admin" } ]
}
);
exit;

or you need to add new user through atlas(refer below snapshot)

  1. Goto Clusters
  2. click Security
  3. Click ADD NEW USER
  4. type username
  5. create Password based on SCRAM-SHA1 method
  6. choose ROLE Atlas admin
  7. click Add User

AddNew-USER

After created user, goto Clusters-->OverView --> CONNECT

  1. Verify the IP Whitelist
  2. Choose a connection method:
    • Connect with Mongo Shell (I am choosing)
    • Connect your Application
    • Connect with MongoDB Compass

$ mongo "mongodb+srv://project-u-s3cgp.azure.mongodb.net/test" --username dbAdmin

MongoDB shell version v3.6.1

Enter password:

connecting to: mongodb+srv://project-u-s3cgp.azure.mongodb.net/test

.........
.........

MongoDB Enterprise project-U-shard-0:PRIMARY>db.user.insert({name:"Daniel"})
WriteResult({ "nInserted" : 1 })
MongoDB Enterprise O2Ci-U-shard-0:PRIMARY>

I hope, you can able to insert the record after creation of root user.

Thanks, Karthick