javascript - Searching in mongodb using just few keywords -
i'm using node js,mongoose,express , mongodb.
i have created simple form, in enter full name , searches in database , return other details .
is possible search using few words out of whole name? e.g. if user wants search "abc xyz",can user same using "abc" or "xyz"
this original route:
app.get("/",function(req,res){ var name = req.query.name; details.findone({fullname:name},function(err,foundasked){ if(err){console.log("error!!!");} else{res.render("details.ejs",{foundasked:foundasked,name:name}); } }); });
is possible that?if yes,then how can implement ?
you use regex partially match on fields so
const query = { fullname: { $regex: req.query.name, $options: 'i' } }; details.find(query, function(err, foundasked){ ... })
where i
in query make case insensitive.
in case of using partial search, recommend not using findone
might misleading user return single result if he/she types let's a
Comments
Post a Comment