mongodb - How to set up mongo db in local and then in remote? -
i have got mongo db commands below link.
i have project , able run it. in project use mongodb, angular js, node js , express server.
i able create user login page.the users stored in mongo db. know users in database.
initially did not install mongodb. how, user , password stored in localhost. hope project having libraries.
but later, in order learn mongo, installed mongo pc. started mongo db , running in link
http://localhost:28017/. mongodb starting : pid=6300 port=27017 dbpath=\data\db\ 64-bit host=new-pc
- question 1
i list out users in db, used project. installation of mongo db me in way? if not without starting mongo db, can know user details saved project?
- question 2
how can set mongo db in remote available in internet?
code
- creating userschema var userschema = new schema({ name: string, email: { type: string, unique: true }, hashed_password: string, provider: string, salt: string, facebook: {}, twitter: {}, github: {}, google: {} }); mongoose.model('user', userschema); - code creating user var mongoose = require('mongoose'), user = mongoose.model('user'), q = require('q'), _ = require('lodash'); exports.create = function(req, res, next) { var requser = new user(req.body); var email = requser.email; if(!requser.email) { res.send(400, {message: 'email mandatory!'}); } var message = null; requser.provider = 'local'; var savepromise = q.when(user.findone({'email': requser.email}).exec()); savepromise.then(function(user) { return requser; if(user == null) { return requser; } else { if(requser.provider!=user.provider && !user.hashed_password) { user.password = requser.password; } else { throw new error("user alredy exists!"); } console.log("in else"); console.log(user); return user; } }, function(err){ console.log("in error 1"); } ).then(function(user){ console.log("in 2"); console.log(user); return user.save(); }, function(err){ console.log("in error 2"); console.log(err); }).then(function(obj){ console.log("in 3"); console.log(obj); }).fail(function(error){ console.log("in fail"); console.log(error); }).done(function(){ // console.log(user); console.log("in done"); }); console.log("returning 400 @ end"); user.save(function(err) { if (err) { console.log(err); message = 'unknowd error occured, please try again!'; if(err.code=11000) { message = 'user email exist. existing user? please login <a href="/signin">here</a>'; user.findone({ email: user.email }, function(err, dbuser) { if (err) { return res.json(400, { message: message }); }else { if(dbuser.provider!=user.provider) { console.log("in if"); console.log(dbuser); if(!dbuser.hashed_password) { console.log("in if"); dbuser.password = user.password; dbuser.save(function(err){ if(err) { console.log(err); } return res.json(dbuser); }); }else { console.log("returning error"); return res.json(400, { message: message }) } } else { return res.json(400, { message: message }); } } }); } }else { return res.json(user); } //console.log("returning success"); }); };
thanks, sabarisri
to answer question #2 first, need either instance of mongodb on own server or hosted one.
see example database-as-a-service. there quite lot of possibilites, check out other offers.
your first question confusing me. helpful see code creates user.
Comments
Post a Comment