"Command find requires Authentication" despite having correct authentication details in mongoURI

Trying to setup MongoDB in Ubunutu VPS for a MERN app.
I’ve enabled authentication in the mongodb settings.
Set up the usernames with password with the roles of read and dbOwner of the target DB.
And I have the mongoURI string setup in the nodeJS server like this:
mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOSTNAME}:${MONGO_PORT}/${MONGO_DB}?authSource={admindb}
In the dry run, without any data in the db, pm2 monit only shows:
“command find requires authentication”
Despite that, mongoURI string from above can log me into the mongo and mongo shell without propblem.
Is there something I’ve forgot?

I would guess you have not correctly set up roles for the MONGO_USERNAME.
A user can log into MongoDB but not be able to do very much if the user does not have certain roles.
If this is your problem, see Built-in Roles and Create a User.

The roles are read and dbOwner.
Both roles should be able to use find function.

If you are able to login with shell can you query your collections or run that find command
For authsource what exactly is the value given

authsource is admin, it’s a different db from MONGO_DB.

Ok
Can you run find query from shell?
Can you show output of db.getUsers()

The problem was resolved when readWrite role added to the user with database access.
Is it because of POST requests?

@Jae_Hong you said in your first post “In the dry run, without any data in the db”.
What sort of find() did you try without any data?
If the find() implied creating a collection in the empty db, I believe you would need readWrite.
Perhaps the problem was related to “without any data in the db”.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.