Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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);
});
 
PREVIOUS NEXT
Tagged: #mongoose #filter #multiple #fields
ADD COMMENT
Topic
Name
1+7 =