My website features a table where users can enter a word in a search field, prompting mongodb to search for matching words in specific objects and only return those results. The search text is included in the request query.
Although the following code handles pagination, it lacks the search functionality.
const result = await orders.aggregate([
{ $facet: {
rows: [
{ $match: { status: {
$in: ['received', 'packing', 'packed', 'fulfilled', 'rejected', 'withdrawn'],
},
} },
{ $skip: offsetPage * parseInt(req.query.rowsPerPage) },
{ $limit: parseInt(req.query.rowsPerPage) },
{ $project: {
record: 1,
status: 1,
onPalette: 1,
note: 1,
grossTotal: 1,
receivedLog: 1,
arrivalDate: 1,
partner: 1,
location: 1,
} },
],
count: [
{ $count: 'count' },
],
} },
]).toArray();
I wish to search within the "record" field, which contains numbers, and in the "partner.name" field, which contains strings.
If no search query is sent, or if the search query (req.query.filter) is an empty string, then return all data.