Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose filter by multiple fields

router.get("/Search", authenticateToken, async (req, res) => {
  let search = req.query.tearms;
  
  // Create expression
  var re = new RegExp(search, "i");
  let find = {};
  let find2 = {};
  if (search != undefined && search != "") {
	//This all are the fields that will used as match
	find = {
      $or: [
        { firstName: { $regex: re } },
        { lastName: { $regex: re } },
        { username: { $regex: re } },
      ],
    };
  }
  let dataSearched = await accounts
    .find(find)
    .select("firstName lastName username profileImage")
    .limit(10);
  res.json(dataSearched);
});
Comment

Mongoose search multiple fields

var re = new RegExp(req.params.search, 'i');

app.User.find().or([{ 'firstName': { $regex: re }}, { 'lastName': { $regex: re }}]).sort('title', 1).exec(function(err, users) {
    res.json(JSON.stringify(users));
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to know the current route in react class component 
Javascript :: (error) = { console.log(error); } 
Javascript :: input from terminal node js 
Javascript :: mongoose find get nested prop only 
Javascript :: tailwincss in react native 
Javascript :: Class constructor cannot be invoked without new 
Javascript :: react comments 
Javascript :: word randomizer 
Javascript :: populate subdocument mongoose 
Javascript :: date without seconds react 
Javascript :: svg in react native 
Javascript :: regex negate 
Javascript :: how to get data form 
Javascript :: template literals in javascript 
Javascript :: after effects loop wiggle 
Javascript :: javascript break and continue 
Javascript :: uncheck checkbox based on id js 
Javascript :: js find in array 
Javascript :: create primary key in mongodb 
Javascript :: convert html to docx javascript 
Javascript :: javascript array push 
Javascript :: popper.js and jquery 
Javascript :: react usememo vs usecallback 
Javascript :: intersection observer api 
Javascript :: Centos install update downgrade nodejs 
Javascript :: manually fire event using javascript 
Javascript :: sort array of objects in ascending order in js 
Javascript :: select option filter javascript 
Javascript :: ckeditor config 
Javascript :: javascript Spread Operator with Object 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =