简体   繁体   中英

“Not authorized on ___ to execute command” with mLab + MongoDB ^3.0

Connects without a hitch, but on insert() throws me this error.

var MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
var url = 'mongodb://____:____@ds125565.mlab.com:25565/heroku_w268n9pj';

MongoClient.connect(url, function(err, client) {
    assert.equal(null, err);
    db = client.db('temp');
    console.log("connected!");
    const collection = db.collection('temp');
    collection.insert([{
        something: please
    }
});

I saw some other answers regarding mLab accounts and credentials, but I just created a new admin account for this. Frustrating because it was working previously with v2.3.

When attempting to connect to an mlab database, you have to correctly specify the client. It's located at the end of your connection string, just after the final forward slash.

mlab_url = "mongodb://db_user_name:db_password@ds139725.mlab.com:39725/heroku_sd1fp182?retryWrites=false"
client = MongoClient(url)


db = client["heroku_sd1fp182"]
collection = db["coinHack"]

You may also get the error:

This MongoDB deployment does not support retryable writes. Please add retryWrites=false to your connection string.

Just add "?retryWrites=false" to your connection string, as shown above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM