I am using mongoDB 2.6.7 and mongoose 3.8.23 driver for node.js. Database is sharded in 2 shards (each is separate replica) and everything looks ok when I enter mongo shell on any of instance, replica, mongos, config servers etc.
After that I have added admin user and user (named normaluser) for specific database I am connecting. I have added them on mongos instance. Users are :
[
{
"_id" : "admin.adminuser",
"user" : "adminuser",
"db" : "admin",
"roles" : [
{
"role" : "dbAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "readWriteAnyDatabase",
"db" : "admin"
},
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
},
{
"role" : "clusterAdmin",
"db" : "admin"
}
]
}
]
and
[
{
"_id" : "mydb.normaluser",
"user" : "normaluser",
"db" : "mydb",
"roles" : [
{
"role" : "readWrite",
"db" : "mydb"
}
]
}
]
From my node.js application I am connecting to mongos instances (I have 2 mongos instances) like this :
mongoose.createConnection('mongodb://mongos-ip-1:27018,mongos-ip-2:27018/mydb', {user : 'normaluser', password : 'some-password', mongos : true, server : {poolSize : 5, keepAlive : true}}
I am successfully connected to database but when I try to query mydb.some-collection from application I get this error :
Possibly unhandled MongoError: not authorized for query on mydb.some-collection
When I go to mongo shell on some of mongos instance and login as normaluser I can query anything and it is ok.
Does anyone knows why I cannot make any query although I have readWrite role to user and specify user in connection url of mongoose? Maybe I am missing something with mongoose?
I have even tried to put dbOwner role to normaluser but nothing changed
Thanks, Ivan
Mongoose relies on the MongoDB Node. js Driver to talk to MongoDB. You can refer to this table for up-to-date information as to which version of the MongoDB driver supports which version of MongoDB.
Mongoose is built untop of mongodb driver, the mongodb driver is more low level. Mongoose provides that easy abstraction to easily define a schema and query. But on the perfomance side Mongdb Driver is best. Save this answer.
Yes, we're actually using multiple drivers, in a production application. We need connections to multiple databases, and mongoose is only able to connect to one DB. So we use MongoDB for the connections to the secondary databases. It should be the same using MongoJS instead.
Mongoose is an object document modeling (ODM) layer that sits on top of Node's MongoDB driver. If your coming from SQL, it's similar to an ORM for a relational database. While it's not required to use Mongoose with the Mongo, here are four reasons why using Mongoose with MongoDB is generally a good idea.
Ivan, try this syntax:
mongoose.createConnection('mongodb://user:pass@localhost:port/db, ...', opts);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With