mongoose-datatables
mongoose-datatables copied to clipboard
SEARCH returns any data which value start with the field value
Hi,
This is my query: 'user':'wer'.
I expected it to return all the data with user named 'wer' only. But instead it return any data where the name started with 'wer' (e.g: werr, werww, wer*). I wish to know if its normal.
below are the code with sources that i found and write at nodejs server. Thank you. `
//https://github.com/archr/mongoose-datatables
// https://github.com/archr/mongoose-datatables/blob/master/example/index.js
Model.dataTables({
limit: req.body.length,
skip: req.body.start,
order: req.body.order,
columns: req.body.columns,
search: {
value: username,
fields: ['user']
},
sort: {
date:-1
}
}).then(function (table) {
res.json({
data:table.data,
recordsFiltered: table.total,
recordsTotal: table.total
});
})
})`
This is to be expected. Its just how the datatable search works.
You could have a look through the datatable docs on their website I'm sure it will clarify.
But is there a way to retrieve only the field with the exact value. I tried to search for but i can't find it yet. Or if you can send me the reference. But at least your answer give me a direction. I'm keep looking for it. Thank you.
edited: nvm, i got it, i have to use regex: https://stackoverflow.com/questions/44003585/datatables-column-search-for-exact-match.
Thanks.
is there any way to make search work?
As @muniekK said he found the solution using regex..
Also see here https://datatables.net/reference/option/search
Also u can can get the data you want directly using mongoDB queries to fetch the data from your DB for example..
Bookings.dataTables({}).then(table => {
Bookings.find({ $and: [{ paidFor: false }, { $or: [{ bookingStatus: 'Booked' }, { bookingStatus: 'Complete' }] }, { paymentDue: { $lt: Date.now() } }] })
.then(unpaidBookings => {
table.data = unpaidBookings || []
return res.json({ data: table.data, recordsFiltered: table.total, recordsTotal: table.total })
}).catch(e => {
console.log('Oh Dear! \n', e, '\n')
res.json({ data: [], recordsFiltered: table.total, recordsTotal: table.total })
})
})
Hope this helps matey.