pimlie / laravel-datatables-mongodb

Laravel DataTables plugin to support Laravel Mongodb
MIT License
12 stars 15 forks source link

Manual search bug #5

Open SKFrozenCloud opened 4 years ago

SKFrozenCloud commented 4 years ago

When doing a manual search with data-tables jQuery plug-in, as shown below:

your_datatable.column('active:name').search(value).ajax.reload();

The query executed includes "%" in the beginning and end of the value you searched for which causes a problem because it is not SQL but rather MongoDB. Some part of the original code gets executed when doing a manual search and adds the "%" which is used in SQL searches.

Here is a sample query when doing a manual search:

mycollection.count({"$and":[{"user_id":1},{"user_id":{"$ne":null}},{"name":{"$regex":{"$regex":".*%Brendan%.*","$options":"i"}}}]})

But when doing global searches it is not included.

Further you can't search numbers. Integers can NOT be searched because it uses Reg-ex and Reg-ex only accepts strings.

EDIT: So somewhere the code has to check if it's an integer and instead of $regex use $match. Also find where ´manual´ searches are executed and remove the added "%".

SKFrozenCloud commented 4 years ago

Also here is my datatables.php config:

'search'         => [
    'smart'            => true,
    'multi_term'       => true,
    'case_insensitive' => true,
    'use_wildcards'    => false,
    'starts_with'      => false,
],

'index_column'   => 'DT_RowIndex',

'engines'        => [
    'moloquent'      => Pimlie\DataTables\MongodbDataTable::class,
    'mongodb-query'  => Pimlie\DataTables\MongodbQueryDataTable::class,
    'mongodb-hybrid' => Pimlie\DataTables\HybridMongodbQueryDataTable::class,

    'eloquent'   => Yajra\DataTables\EloquentDataTable::class,
    'query'      => Yajra\DataTables\QueryDataTable::class,
    'collection' => Yajra\DataTables\CollectionDataTable::class,
    'resource'   => Yajra\DataTables\ApiResourceDataTable::class,
],

'builders'       => [
    Jenssegers\Mongodb\Eloquent\Builder::class             => 'moloquent',
    Jenssegers\Mongodb\Query\Builder::class                => 'mongodb-query',
    Jenssegers\Mongodb\Helpers\EloquentBuilder::class      => 'eloquent',
],