I have Training Collection with lots of data. All function works except that on search result the latest entry results appear correctly, but the old results don't appear on front except they appear on the pages based on their position. Helper and course are populated from another schema. The reason I used the if condition is because the helper and course are populated. I tried searching for similar problem on stack overflow and other website and tried to query it as 'helper.first_name': { $regex: reg_ex } but it did not work for me that way. Here is the pseudo code below P.s I am using pagination. I need help please
req.checkBody('search').notEmpty().withMessage('Please enter a query'); // check empty entry
const reg_ex = new RegExp(req.body.search, 'i'); // regular expression
const page = parseInt(req.query.page);
const limit = 20;
const skipIndex = (page - 1) * limit;
try {
Training
.find()
.populate([{
path: 'helper',
model: Helper
},
{
path: 'course',
model: Course
},
{
path: 'user',
select: '-password'
}
])
.sort({
_id: -1
})
.limit(limit)
.skip(skipIndex)
.exec(function (err, doc) {
if (err) {
return next(err)
}
const arryPush = [];
doc.forEach(data => {
if (
(data.batch.match(reg_ex) != null) ||
(data.helper.first_name.match(reg_ex) != null) ||
(data.helper.last_name.match(reg_ex) != null) ||
(data.helper.phone.match(reg_ex) != null) ||
(data.helper.email.match(reg_ex) != null) ||
(data.course[0].title.en.match(reg_ex) != null)
) {
arryPush.push({
_id: data._id,
course: [{
title: {
en: data.course[0].title.en,
am: data.course[0].title.am
}
}],
is_rejected: data.is_rejected,
approved: data.approved,
training_completion_status: data.training_completion_status,
training_time: data.training_time,
weekdays: data.weekdays,
payment_option: data.payment_option,
batch: data.batch,
created_at: data.created_at,
helper: {
first_name: data.helper.first_name,
last_name: data.helper.last_name,
phone: data.helper.phone,
email: data.helper.email
}
})
}
});
res.status(200).json(arryPush)
});
} catch (e) {
res.status(500).json({
message: "Error Occured" + e
});
}