javascript - querying MongoDb with nodejs -
i have situation here. have ajax call used query value , return. in js file gave this
function getlogindata(){ debugger; var getusername=$("#inputusername").val(); $.ajax({ url:'/users/loginvalidation', datatype:'json', data:{"uname":getusername}, type:'post', async:false, success:function(data){ debugger; } }) }
in users.js file in routes gave this
router.post('/loginvalidation',function(req,res){ var db=req.db; var getname=req.body.uname; db.collection("users").find({"name":getname},{"type":{$in: [ "admin", "owner" ]}}).toarray(function(err,result){ res.json(result); }) })
- my requirement want check in users collection containing name in "getname" variable
- along want check whether name of type "admin" or "owner".
- if both these conditions don't satisfy return error message
my user collection this
{ "_id" : 1, "name" : "rohith", "type" : "admin" } { "_id" : 2, "name" : "kumar", "type" : "owner" } { "_id" : 3, "name" : "krishna", "type" : "sales" } { "_id" : 4, "name" : "nikhil", "type" : "sales" } { "_id" : 5, "name" : "don", "type" : "admin" }
now getting null value in success of ajax call... thanks
use $and, this:
db.collection("users") .find({"$and": [ {"name": getname}, {"type": {"$in": [ "admin", "owner" ]} ]}) .toarray(function(err,result){ if (!err && !result) err = 'no results'; if (err) return res.send(500, err); res.json(result); })
Comments
Post a Comment