B T
B T

Reputation: 60925

How do I correctly authenticate in mongo for a user that has access to multiple databases?

I have a user with the role: {role: 'root', db:'admin'} which should have access to all the databases in my mongo instance. I'm using mongoskin in node.js to interact with mongo. My problem is that it isn't correctly accessing my databases. If I authenticate with

mongodb://owner:mylocalpassword@localhost:27017/mydatabase

It simply gives me MongoError: Authentication failed. If I instead auth with:

mongodb://owner:mylocalpassword@localhost:27017/admin

it authenticates, but I can't access mydatabase.

Here's my connection code:

var connection = mongoskin.db("mongodb://owner:mylocalpassword@localhost:27017/admin", {journal:true, auto_reconnect:true})

I assume that since I'm accessing the admin database there, that's the only one it interacts with. So I tried do then do this:

var mydatabaseConnection = connection.db('mydatabase')

But when I use that, my app is returning no results for queries on collections that I know have data. What am I doing wrong here? How do I get this user to access a database other than admin?

Upvotes: 0

Views: 81

Answers (1)

B T
B T

Reputation: 60925

Ok, so I found out that mongoskin's db method simply doesn't work. Finally I'm forced to completely remove mongoskin from my codebase. The real answer here is don't use mongoskin.

This code worked with mongo native:

MongoClient.connect("mongodb://owner:mylocalpassword@localhost:27017/admin", {journal: true, auto_reconnect:true}).then(function(db) {    
    console.log("Connected!")

    var mydb = db.db('mydatabase')

    var User = mydb.collection('User')
    return User.find({}).toArray().then(function(users) {
        console.log(users)
        db.close()        
    })
}).catch(function(e) {
    console.log(e.stack)
})

Upvotes: 1

Related Questions