I have generated a MongoDb database named "mydb" and a collection called "mydb". And I also generated a db user and granted that user these roles :
db.updateUser("myadmin", {pwd:"mypasswd", roles:[{role :"readWrite", db: "admin"},{role : "readWriteAnyDatabase", db: "admin"},{role : "userAdminAnyDatabase", db: "admin"},{role : "dbAdminAnyDatabase", db: "admin"}]})
I am starting mongod like this :
mongod --auth --dbpath C:\data\db
I am starting mongo like this :
mongo --port 27017 -u "myadmin" -p "mypasswd" --authenticationDatabase "admin"
Then I'm connecting to admin db like this :
var MongoClient = require('mongodb').MongoClient;
// Connect to the db
MongoClient.connect("mongodb://x.x.x.x:27017/admin", function(err, db) {
if(!err) {
console.log("successfully connected to the database");
}else{
console.log("Error on connecting... aborting and exiting");
return console.dir(err);
throw err;
}
db.authenticate('myadmin', 'mypasswd', function(err, res) {
if(!err) {
console.log("authenticate ok");
}
else{
console.log("Error on connecting... aborting and exiting");
return console.dir(err);
throw err;
}
});
});
Now I need to work with "mydb" database. Can you tell me how I should modify my code so that I can work with "mydb"? Thanks in advance.