Query randomly errors with Unauthorized for certain simple query

So far everything works fine with Atlas, just one simple query errors with Unauthorized Code 13 every 30~ call.

We already tried to give the user the right readAnyDatabase, but even that doesnt help. Still every 30~ call errors. It makes totally no sense since most of the calls work without an error. Also all other queries are not affected, only this one.

The query itself is very simple:

wait db.collection("locations").find({
            "owner": {
                $ne: userid,
                location_data: {
                    type: 1,

Please provide the complete error message. Code 13 is not sufficient. There are important information, like database, collection and actions from the error message that we need to know to pinpoint the issue.

Unfortunately the catched error doesn’t provide more information:


Indeed it does not provide much. Which server and driver versions are you using? Usually we get the database name and the operation.

There is no reasons for such a behaviour. I suspect that your variable db is changed by some other functions. At this point I would catch the error and add some tracing so that you can confirm that db is not changed and that the client has not been re-authenticated with some other credentials.

It’s an M2 instance with 4.4.9 and driver is nodejs 4.0.1

The db variable is only set at one place and never changed. Also with a non atlas mongodb server everything works. This just happens with atlas.

May be it is related to the daily restart of your M2 instance. See Database daily restart/disconnection - #3 by Stennie

For what it’s worth, maintenance is not a daily occurrence and whrn it does occur it happens in a rolling manner including a mongodb replica set level election where the app driver will automatically discover the new primary and continue on its way.

If you are experiencing speed bumps along the way, I encourage you to open up a support case and reach out for help