node.js - Connect to MongoDB with authentication using Node JS -
i trying connect mongodb user/password , did far:
var mongoclient = require('mongodb').mongoclient; // connect db mongoclient.connect("mongodb://${host}:27017/${db}", function(err, db) { if(!err) { console.log("successfully connected database"); }else{ console.log("error on connecting... aborting , exiting"); return console.dir(err); throw err; } db.authenticate('username', 'password', function(err, res) { console.log("reached here"); }); });
now trying login inside data base in order able inside mongo database's collections, how can that?
thanks!
you can perform curd operations following:
var mongoclient = require('mongodb').mongoclient; // connect db mongoclient.connect("mongodb://${host}:27017/${db}", function(err, db) { if(!err) { console.log("successfully connected database"); //here can perform operation var collection = db.collection('test'); var doc1 = {'hello':'doc1'}; var doc2 = {'hello':'doc2'}; collection.insert(doc1); }else{ console.log("error on connecting... aborting , exiting"); return console.dir(err); throw err; } db.authenticate('username', 'password', function(err, res) { console.log("reached here"); }); });
Comments
Post a Comment