Home > Back-end >  How can query string search a data using mongoose?
How can query string search a data using mongoose?

Time:11-01

Im using mongoose-paginate for my pagination and I want to add search for my project... How can get query string search using mongoose node like these example... (/allStudents?stdID=440033)

My code is:

router.get('/allStudents', async (req,res) => {

  const limit = parseInt(req.query.limit, 4) || 4;
  const page = parseInt(req.query.page, 10) || 1;

  const PAGE_SIZE = 10;
  const skip = (page - 1) * PAGE_SIZE;

  try {

     const students = await Student.paginate({}, {limit, page, skip}) 
        ||  await Student.find({ stdID:  req.query.stdID})

     return res.status(200).json({
        success: true,
        data: students
     })

 } catch (err) {
       console.log(err);
 }


});

CodePudding user response:

You should pass your query as the first parameter according to the documentation:

const students = await Student
    .paginate(
        {stdID: req.query.stdID},
        {limit, page, skip}
    );
  • Related