Cant read or write to atlas database

Im new to mongodb, can someone explain to me how can i authenticate using SCRAM?

I tried writing to the database, but it returned this error: UnhandledPromiseRejectionWarning: MongoServerError: (Unauthorized) not authorized on admin to execute command

This is my code for the client:

 const { MongoClient, ServerApiVersion } = require('mongodb');
  const uri = "mongodb+srv://QuackDev:" + password + "@rocketer.iolndfg.mongodb.net/?retryWrites=true&w=majority";
  const client = new MongoClient(uri, {
    serverApi: {
      version: ServerApiVersion.v1,
      strict: true,
      deprecationErrors: true,
    }
  });

Am i doing something wrongly?

It does not work, so yes you are doing something wrong. But the issue is simply because you are trying to read or write in the admin database rather than your database.

Look at https://www.mongodb.com/docs/manual/reference/connection-string/ to see how to specify a different database.

1 Like

That resolved my issue :sweat_smile:
Thanks for your help!

2 Likes