mongoose-datatables icon indicating copy to clipboard operation
mongoose-datatables copied to clipboard

SEARCH returns any data which value start with the field value

Open muniekK opened this issue 6 years ago • 5 comments

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
    }); 
  })
})`

muniekK avatar May 26 '18 18:05 muniekK

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.

VonSwirl avatar May 27 '18 08:05 VonSwirl

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.

muniekK avatar Jun 05 '18 00:06 muniekK

is there any way to make search work?

Mohit8982 avatar Dec 13 '19 12:12 Mohit8982

As @muniekK said he found the solution using regex..

Also see here https://datatables.net/reference/option/search

VonSwirl avatar Dec 13 '19 12:12 VonSwirl

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.

VonSwirl avatar Dec 13 '19 13:12 VonSwirl