GithubHelp home page GithubHelp logo

mongoose-datatable's Introduction

mongoose-dataTable

Version 3.0.0

Server side dataTable request support for mongoose.

Support mongoose version >= 3.8.0

Support mongoDB version >= 2.4

Support DataTable >= 1.10

New Functionnalities

  • Population of ref sub-document or ref sub-documents are done with $lookup in aggregation
  • Filter and sort are possible on populated sub-documents
  • add regex search on string fields

Migration

v1.x.x

var DataTable = require('mongoose-datatable');
var options = { debug: true, verbose: true };
DataTable.configure(options);
mongoose.plugin(Datatable.init);

...

model.dataTable(query, {}, function(err, data) {
  if (err) { return manageError(err); }
  console.log('send data...');
  res.send(data);
});

v2.x.x [typescript]

import Datatable from './datatable';
const nologger = { debug: () => { }, info: () => { }, warn: () => { }, error: () => { } } as any;
var options = { logger: nologger };
Datatable.configure(options);
mongoose.plugin(Datatable.init);

...

model.dataTable(query)
  .then((data: any) =>  res.send(data))
  .catch(err => manageError(err))

Install

npm install mongoose-datatable

Loading

import Datatable from './datatable';

Configuration

DataTable.configure(options);

Options

Configuration is not mandatory, the default options contains only the default handlers, for other types, an unknown type message is displayed on the console.

Logger

option key: logger If the options contains a logger [key: logger]: ILogger, the default logger console is override by it. It allows to use an application specific logger.

interface ILogger {
  debug: (...data: any) => void;
  info: (...data: any) => void;
  warn: (...data: any) => void;
  error: (...data: any) => void;
}

Condition handlers

option key: handlers The condition handlers is an object that contains handlers (functions) with for key, mongoose schema field type name. eg. String, Date, Boolean... It is possible to declare a default handler (key: default) that will be used if no handler found for a specific type. To not do any search the function should return null. These handlers are called when the module try to build the condition on a field. They can return a condition object for the field to match ( eg.: { $in: [...] } ) and have for arguments:

  • options: IOptions

    The options passed to the model database call.

  • query: IQuery

    The query passed to the model database call.

  • column: IColumn

    The jquery column data.

  • field: any

    The field against the condition is build. This is the mongoose FieldType.

  • search: ISearch

    The search data to build the condition.

  • global: boolean

    If true, the search condition is from jquery global search; if false, it's a specific search on thie field.

Default condition handlers
  • String

    • global
      Match anywere in the string in case insensitive mode.

    • specific
      If the search string is a regex (match: /^/./$/*) then the regex is used
      Else match anywere in the string in case insensitive mode.

  • Boolean

    • global
      No search done

    • specific
      Match true or false in case insensitive mode.

  • Date

    • global
      No search done

    • specific
      The date search is composed in three parts, the type of the match, the from value and the to value. The from and to value are String dates and the to value is only needed when the type is "<>" or "<=>". The type can be "=" (same as no type), ">", ">=", "<", "<=", "<>", "<=>" meaning respectively equals, greater than, greater or equal than, less than, less or equal than, between and between equals.

  • Number

    • global
      No search done

    • specific
      The number search is composed in three parts, the type of the match, the from value and the to value. The from and to value are number and the to value is only needed when the type is "<>" or "<=>". The type can be "=" (same as no type), ">", ">=", "<", "<=", "<>", "<=>" meaning respectively equals, greater than, greater or equal than, less than, less or equal than, between and between equals.

eg.
options: {
  handlers: {
    String: StringHandler,
    Boolean: booleanHandler,
    Date: dateHandler,
    default: defaultHandler
  }
}

Initialization

const mongoose = require('mongoose');
mongoose.plugin(DataTable.init);

Fields

If a mongoose schema field is marked as not selectable with the option "select: false". Or if the option dataTableSelect is present and set to false: "dataTableSelect: false". Then the field will not be selected even if it was requested by the dataTable client.

Usage

The method datatable was added to all the schema as static method. The method has for parameters:

  • query

    The query parameters send by the dataTable client

  • options

    Options pass to the condition handlers. OPTIONAL parameter.

    • handlers

      Handlers can be given to override the overall behavior of condition builder handlers. The field options.handlers is an object with for keys either a field type (like String, Boolean,,,) or a field path (like username, name.firstName) and for values, functions like the condition handler functions.

    • conditions

      Conditions is an object as the mongoose find conditions. This conditions filter the dataTable data returned and the counts, it is applied as the first conjunction condition.

    • select

      Select is an object, a string or an array as the mongoose query select argument. The select is applied on the find query that return the displayed entities.

  • callback

    The callback called in case of error or when the data have been retrieved.

model.dataTable(query, options)
  .then((data: any) =>  res.send(data))
  .catch(err => manageError(err))

Chunk Search

- Not implemented yet

Change log]

  • v2.1.0
    • null search value are now intepreted as search for null value on field

mongoose-datatable's People

Contributors

adeiji avatar eherve avatar jlestel avatar lepazmino avatar mulyoved avatar ricardofbarros avatar somnathdapl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mongoose-datatable's Issues

MongoError: exception: dotted field names are only allowed at the top level

Okay so I have my model structure like this for example

phones : {
    number : String,
    campaign_id: Number,
    usedBy : [{
       some_field : String,
       time : Date
    }],
    insertedBy: [{
        numberDestiny : String
        time : Date
    }]
}

And Im trying to filter 'insertedBy.time', and your plugin constructs this query

db.phones.aggregate(
[
 { '$match': { '$and': [ { 'insertedBy.numberDestiny': { '$in': [ /test/i ] } } ] } },
  { '$sort': { number: 1 } },
  { '$skip': 0 },
  { '$limit': 10 },
  { '$unwind': '$insertedBy' },
  { '$match': { 'insertedBy.numberDestiny': { '$in': [ /test/i ] } } },
  { '$group': 
     { _id: 
        { _id: '$_id',
          number: '$number',
          campaign_id: '$campaign_id',
          'usedBy.callcenter_id': '$usedBy.callcenter_id',
          'usedBy.time': '$usedBy.time' },
       insertedBy: { '$push': '$insertedBy' } } },
  { '$project': 
     { _id: '$_id._id',
       insertedBy: 1,
       number: '$_id.number',
       campaign_id: '$_id.campaign_id',
       'usedBy.callcenter_id': '$_id.usedBy.callcenter_id',
       'usedBy.time': '$_id.usedBy.time' } },
  { '$project': 
     { number: 1,
       'insertedBy.numberDestiny': 1,
       campaign_id: 1,
       'insertedBy.time': 1,
       'usedBy.callcenter_id': 1,
       'usedBy.time': 1 } } 

]

)

which gives me this nasty error,

Error("Printing Stack Trace")@:0
()@src/mongo/shell/utils.js:37
([object Array])@src/mongo/shell/collection.js:866
@(shell):30

uncaught exception: aggregate failed: {
    "errmsg" : "exception: dotted field names are only allowed at the top level",
    "code" : 16405,
    "ok" : 0
}

Well I tweaked (joined 2 fields) the query generated from your plugin and got to something like this, which the output given is perfect

db.phones.aggregate(
[
 { '$match': { '$and': [ { 'insertedBy.numberDestiny': { '$in': [ /tes/i ] } } ] } },
  { '$sort': { number: 1 } },
  { '$skip': 0 },
  { '$limit': 10 },
  { '$unwind': '$insertedBy' },
  { '$match': { 'insertedBy.numberDestiny': { '$in': [ /tes/i ] } } },
  { '$group': 
     { _id: 
        { _id: '$_id',
          number: '$number',
          campaign_id: '$campaign_id',
          usedBy : '$usedBy' },
       insertedBy: { '$push': '$insertedBy' } } },
  { '$project': 
     { _id: '$_id._id',
       insertedBy: 1,
       number: '$_id.number',
       campaign_id: '$_id.campaign_id',
       'usedBy.callcenter_id': '$_id.usedBy.callcenter_id',
       'usedBy.time': '$_id.usedBy.time' } },
  { '$project': 
     { number: 1,
       'insertedBy.numberDestiny': 1,
       campaign_id: 1,
       'insertedBy.time': 1,
       'usedBy.callcenter_id': 1,
       'usedBy.time': 1 } } 

]

)

My question here, is there any "right" way to do this type of stuff with your plugin ?

This can be usefull, the req.query has this keys,

  mDataProp_1: 'insertedBy.numberDestiny',
  sSearch_1: '3443',
  bRegex_1: 'false',
  bSearchable_1: 'true',

I just dont know if this is the proper way to send the query string to work with your plugin, If not I'm going to make pull request to resolve this issue.

tableTools print not working

The tableTools extension sets iDisplayLength to -1 for printing the table. This causes mongoose-dataTables to return undefined data.

Easy fix in DataTables.js (around line 138):

Replace:
query.skip(searchCriteria.pageStart).limit(searchCriteria.pageSize);

With:
if (searchCriteria.pageSize >= 0) {
query.skip(searchCriteria.pageStart).limit(searchCriteria.pageSize);
}

Select not working

I'm doing something like this

    var options = {};
    options.select = 'id campaign number timeInserted usedBy';
    model.dataTable(req.query, options, function(err, data) {
        res.json(data);
    });

And I can't select those fields I only can select them, doing something, on my frontend like this :

$('#data-Table').dataTable({
  "bProcessing" : true,
  "bServerSide" : true,
  "sAjaxSource" : '{{ route("phone_list.list") }}',
  "aoColumns" : [
    { "mData" : "id", "bVisible": false },
    { "mData" : "number" },
    { "mData" : "campaign" },    
    { "mData" : "timeInserted" },
    { "mData" : "usedBy" },
    { 
      "sName": "RoleId",
      "bSearchable": false,
      "bSortable": false,
      "mRender": function (data, type, full)                            
      {
        var id = full['id'];
        var route = '{{ route('phone_list.view')."/"}}';
        return "<a href='"+route+id+"'>"+
        "<button class='btn btn-primary btn-circle'>"+
        "<i class='fa fa-list' style='font-size:18px'></i></button> </a>";
      }
     }
  ]
});   

Is this normal/expected behaviour ?

Query lookup another collection, if not found still return

I want to ask, I want to query to join another collection. It's working. But, I want to handle if I can't found that object id in another collection and the reality it's still return data with value of field is null. Now, I don't want return that data if join/lookup/populate not found. How to solve this? Can you help me?

how to do subquery in formatter

`Model.dataTables({
limit: input.length,
skip: input.start,
search: {
value: req.body.search.value,
fields: ['name']
},
sort: orderBy,
formatter: function(table) {
let permission_html = "";
_.each(table.permission,function(p){
permission_html = permission_html + ""+ p.name +""
})

        var count = 0 
            count = await Admin.countDocuments({'roles':table._id}).exec() 
        console.log("count")
        console.log(count)

        return {
            actions     : '<a href="/admin/role/update/'+table._id+'"><i class="fas fa-edit"></i></a><a data-id="'+table._id+'" href="javascript:void(0)" class="delete_role"><i class="fas fa-trash"></i></a>',
            id          : table._id,
            _id         : i++,
            name        : table.name,
            permission  : permission_html,
            created_at  : moment.unix(table.created_at).format("DD-MM-YYYY H:s"),
        }            
    }        
}).then(function (table) {
    /*console.log("table")
    console.log(table)*/
    table.draw = table.draw
    table.recordsTotal = table.total
    table.recordsFiltered = table.total
    res.json(table); // table.total, table.data
})`

i want count how much admin hase this role

Use aggregate with datatable

I am using datatable with mongoose.
I need to use an aggregate lookup to join another table, but it not returning any data.

Issue with number handlers

So far I've only tested with numbers, but what is happening is If I'm sending a string of a regex in a field which the type is Number

  mDataProp_3: 'insertedBy.provider_id',
  sSearch_3: '^(2)$',
  bRegex_3: 'true',
  bSearchable_3: 'true',
  bSortable_3: 'true',

and your plugin is parsing that string to field.search.search, so far so good, but it converts from a string to a reg exp object

and in your conditionBuilder.js

you have this if

  if (field.search.search && field.search.search.length > 0)
    disjunction.push(field.search.search);

The problem is that field.search.search is a obj reg exp and you will never catch it length, so numbers will never enter in numberHandler, I'm going to push a fix for this isue, feel free to evaluate my solution.

Thanks, bye 👍

Default Boolean handler not works for false value

Hi @eherve,
The default booleanHandler returns false when filtering for 'false' value so the conditions field is undefined, maybe because no conditions is added:

// Default boolean handler
function(field, search) {
  var value;
  search.forEach(function(chunk) {
    if (/^true$/i.test(chunk)) {
      value = true;
    } else if (/^false$/i.test(chunk)) {
      value = value || false;
    }
  });
  return value == undefined ? undefined : value;
}

// addConditions in searchCriteria
function addConditions(searchCriteria, field, conjunctions, disjunctions) {
  var condition = ConditionBuilder.getGeneralSearch(field,
      searchCriteria.search, searchCriteria.options);
  if (condition) {
    var element = {};
    element[field.path] = condition;
    disjunctions.push(element);
  }
  condition = ConditionBuilder.getFieldSearch(field, searchCriteria.options);
  if (condition) {
    var element = {};
    element[field.path] = condition;
    conjunctions.push(element);
    if (field.arrayPath) addAggregate(searchCriteria, field, condition);
  }
}

I have overwrite the default boolean handler with my own version and works right. Just changed the last line to return {$in: [value]}

  options.handlers = {
    Boolean: function (field, search) {
      var value;
      search.forEach(function (chunk) {
        if (/^true$/i.test(chunk)) {
          value = true;
        } else if (/^false$/i.test(chunk)) {
          value = value || false;
        }
      });
      return value == undefined ? undefined : {$in: [value]};
    }
  };

Also if we want consider 'false' as 'not true' we can use this other version:

  options.handlers = {
    Boolean: function (field, search) {
      var value;
      search.forEach(function (chunk) {
        if (/^true$/i.test(chunk)) {
          value = true;
        } else if (/^false$/i.test(chunk)) {
          value = value || false;
        }
      });
      return value == undefined ? undefined : {$in: value ? [value] : [null, value]};
    }
  };

What do you think to include one of the proposed boolean handlers as the default?

Example

Hi, I like to get this work, but somehow searches not work at all. Could you provide full example, thanks.

No data returned on mongoose 5 with options.conditions

Hi,

I have updated to mongoose 5 and no results are returned when I use the options.conditions parameter

Tests made with mongoose 4.13.12 with options.conditions

{
  draw: '7',
  recordsTotal: 90,
  recordsFiltered: 90,
  data: [ ...25 Objects... ]
}

Tests made with mongoose 5.0.15 with options.conditions

{
  draw: '12',
  recordsTotal: 90,
  recordsFiltered: 90,
  data: []
}

Tests made with mongoose 5.0.15 without options.conditions

{
  draw: '5',
  recordsTotal: 2689,
  recordsFiltered: 2689,
  data: [ ...25 Objects... ]
}

Best Regards,
Daniel

Formula

Allow the data table to activate formula search and resolves the search string as formula with operators ($or, $and, ...) and field refferencing.

Fake requests to get other fields. Options.select not working

Hi!
Thanx for Your plugin.

I have issue with hiding the unnesesary fields from response.

According to docs I can select fields that I want to send to client:

Model.dataTable(req.query, {
    conditions: conditions,
    select: 'name'
}, callback)

But I can get every field from collection by faking this request:

?sEcho=1&iColumns=2&sColumns=&iDisplayStart=0&iDisplayLength=10&mDataProp_0=name&mDataProp_1=created&sSearch=&bRegex=false&sSearch_0=&bRegex_0=false&bSearchable_0=true&sSearch_1=&bRegex_1=false&bSearchable_1=true&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&bSortable_0=true&bSortable_1=true&_=1412322544184
{
  "sEcho": "1",
  "iTotalRecords": 1,
  "iTotalDisplayRecords": 1,
  "aaData": [
    {
      "_id": "542bfd18425c0d2d3xxxxx",
      "name": "for test",
      "created": "2014-10-01T13:09:44.632Z" <----- I want to hide this field
    }
  ]
}

Ii it neccessary to parse get request before calling the plugin?
Is it a bug or something wrong with my code?
Thanx!

search not working with populate data.

==============================Controller==============================
router.get('/matchdata',function(req, res){
Match.dataTable(req.query,{conditions: {status: 1}}, function (err, tdata) {
var dataRow = '';
var ndata=[];
tdata.data.forEach(function(value,index){
console.log("value is " + value +' '+ "index is" +index);
if (!value.date) {
var date='No Date';
}else{
var date=value.date;
}

    var obj=value;
    var obj2={date:date};
    _.merge(obj, obj2);
    ndata.push(obj);
}); 
_.merge(tdata, ndata);
res.send(tdata);

});
});

==================================html===============================
var table=$('#sampleTable').dataTable({
processing : true,
serverSide : true,
order: [[ 6, 'desc' ]],
ajax : { url: '/admins/matchdata' },
columns : [
{'data': 'user_id.name'},
{'data': 'user_id.gender'},
{'data': 'user_id.country'},
{'data': 'liked_user_id.name'},
{'data': 'liked_user_id.gender'},
{'data': 'liked_user_id.country'},
{'data': 'date'}
]
});

============================Model===============================
var mongoose = require('mongoose');
var DataTable = require('mongoose-datatable');
mongoose.connect('mongodb://111.93.127.5:27015/Sophy_App',{ useMongoClient: true });
var Schema = mongoose.Schema;
var db = mongoose.connection;

var matchSchema=mongoose.Schema({
user_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'User' },
liked_user_id:{ type: mongoose.Schema.Types.ObjectId, ref: 'User' },
status:{
type:String
},
date: {
type:String,
default:''
}

});

DataTable.configure({ verbose: true, debug : true });
mongoose.plugin(DataTable.init);
var Match = mongoose.model('Match',matchSchema);
var MyModel = module.exports = require('mongoose').model('Match');

Why i tries to serach name its not showing any record. please help me

Mongoose Queries

This plugin can use mongoose queries?. If it can - how to do it ? need help asap

Verbose mode

Actually when the default options for building condition is used, only the String field of mongoose schema condition is build, the other types display a log message on the console. This message should be displayed only if the option "verbose" is true in the configuration.

searchBuilder.js has some bug.

I have used mongoose-datatable very well.
However, updated to version 1.0.1 broken my app.
Because isSearchFieldRegexp function in searchBuilder.js has code like..

function isSearchFieldRegexp(index) {
  return query[QUERY_COLUMNS][index][QUERY_SEARCH][QUERY_SEARCH_REGEXP_PREFIX] === 'true' ||
    uery[QUERY_COLUMNS][index][QUERY_SEARCH][QUERY_SEARCH_REGEXP_PREFIX] === true;
}

but uery should be query like..

function isSearchFieldRegexp(index) {
  return query[QUERY_COLUMNS][index][QUERY_SEARCH][QUERY_SEARCH_REGEXP_PREFIX] === 'true' ||
    query[QUERY_COLUMNS][index][QUERY_SEARCH][QUERY_SEARCH_REGEXP_PREFIX] === true;
}

Please fix this issue. Thank you.

Unmanaged condition on field type: ObjectId

Hi there,

when i search for path "_id" the type is "ObjectId" but for it doesn't exist a Handler.
Is that intentional.
I added myself a "objectIdHandler.js", because I want to search for ObjectId, is there any other possibility.

regards

Chunk search keywords

Allow the use of keywords like $or, $not, $in, $gt, $ge, $lt, $le...
with a value bracet and separator ?

chunk search template

Update the chunk search template to take everything after the ":" beside space or tab as the search value.

Data from populate not sorting

Hi,

I installed your module, works ok with two models that are connected between them, but when I try to sort by that column, is not working, although the query builds ok, I see this in debug:

populate:
   [ { path: 'username',
       sort: { name: 'asc' },
       select: 'name' } ] }

What can be the issue?

Date filtering

Hi Eric,
I'm trying to find good mongoose datatable plugin, yours are much mature than others, noticed no updated couple years, not sure if you are still watching.
What I want to do is that, I have a field with Date type, when I do filtering, say I input "2018" in the data table search box, your mongoose-datatable won't filter out all 2018 items I expected, I understand that you are using old isDate to check date parsed "2018" which return false. any easy fix which send back the expected result?

Best Regards
Steven

Column filtering/searching with regex

Hi there,

Great work on this project.

I can't get the column regex search to work, I've tried sending a Regexp and also a regex as a string from datatables. And applying true to the regex attribute.

Nevertheless it doesn't seem to work, any advise on this?

Thanks in advance!

sSearch with date ranges

Hello again,

I'm sniffing the code, I got to queryBuilder.js and I notice you ignore the sRangeSeparator param and I don't quite understand how you build the query, basically I'm using this add-on for data tables, strings work fine, but If I include dates, it just breaks, let me dump some of my requests sniffed with Firebug

_   1405941869040
bRegex  false
bRegex_0    false
bRegex_1    false
bRegex_2    false
bRegex_3    false
bRegex_4    false
bRegex_5    false
bSearchable_0   true
bSearchable_1   true
bSearchable_2   true
bSearchable_3   true
bSearchable_4   true
bSearchable_5   false
bSortable_0 true
bSortable_1 true
bSortable_2 true
bSortable_3 true
bSortable_4 true
bSortable_5 false
iColumns    6
iDisplayLength  10
iDisplayStart   0
iSortCol_0  0
iSortingCols    1
mDataProp_0 number.origin
mDataProp_1 campaign
mDataProp_2 usedBy.callcenter
mDataProp_3 timeInserted
mDataProp_4 usedBy.timeConsumed
mDataProp_5 5
sColumns    ,,,,,RoleId
sEcho   1
sSearch 
sSearch_0 test  
sSearch_1   
sSearch_2   
sSearch_3   
sSearch_4   
sSearch_5   
sSortDir_0  asc

This work just fine, and searches test on first column, but If I include date ranges on my HTML+JS, I will get something like this:

bRegex  false
bRegex_0    false
bRegex_1    false
bRegex_2    false
bRegex_3    false
bRegex_4    false
bRegex_5    false
bSearchable_0   true
bSearchable_1   true
bSearchable_2   true
bSearchable_3   true
bSearchable_4   true
bSearchable_5   false
bSortable_0 true
bSortable_1 true
bSortable_2 true
bSortable_3 true
bSortable_4 true
bSortable_5 false
iColumns    6
iDisplayLength  10
iDisplayStart   0
iSortCol_0  0
iSortingCols    1
mDataProp_0 number.origin
mDataProp_1 campaign
mDataProp_2 usedBy.callcenter
mDataProp_3 timeInserted
mDataProp_4 usedBy.timeConsumed
mDataProp_5 5
sColumns    ,,,,,RoleId
sEcho   4
sRangeSeparator ~
sSearch 
sSearch_0   ds
sSearch_1   
sSearch_2   
sSearch_3   ~
sSearch_4   ~
sSearch_5   
sSortDir_0  desc

I will give you my table structure so you can get an idea

                        <thead>
                            <tr>
                                <th>N&uacute;mero</th>
                                <th>Campanha</th>
                                <th>Callcenter</th>                                
                                <th>Data Inserido</th>                                
                                <th>Data Consumido</th>
                                <th class="no-sort">A&ccedil;&otilde;es</th>
                            </tr>
                        </thead>

I'm going to try to include dates and sRangeSepartator support to your package.

Sort column doesn't work in new 1.0.0 version

Hi,

I upgrade your module form 0.4.2 to 1.0.0 with datatable 1.10.5 and I see that columns ordering doesn't work :-(

I see in your code reference to sortable but in 1.10 this parameter was named orderable. Is the problem ?
Thank for your help

Herve

mongo query filters in Model.dataTable(query,callBAck)

How can i include other filters in query for data in Model.dataTable(query,callBAck)?
for example i want to apply following filters to the query while fetching data:
[ { '$match':
{ 'xform.id': '55c4a5d97a7556b6348f7bd9',
'meta.end':
{ '$gte': Wed Aug 05 2015 05:30:00 GMT+0530 (IST),
'$lte': Thu Aug 13 2015 05:30:00 GMT+0530 (IST) },
'meta.center':
{ '$in':
[ '550281ffc07b90fe1c4b6002',
'5502aee31bb1f8903225d1c3',
'556e88c3d727fa500e4b1bbe' ] } } } ]
Please reply asap, thanks in advance.

MongoError: exception: the limit must be positive

hello, when the client send a query like this:
draw:1
columns[0][data]:0
columns[0][name]:
columns[0][searchable]:true
columns[0][orderable]:false
columns[0][search][value]:
columns[0][search][regex]:false
columns[1][data]:1
columns[1][name]:
columns[1][searchable]:true
columns[1][orderable]:true
columns[1][search][value]:
columns[1][search][regex]:false
columns[2][data]:2
columns[2][name]:
columns[2][searchable]:true
columns[2][orderable]:true
columns[2][search][value]:
columns[2][search][regex]:false
columns[3][data]:3
columns[3][name]:
columns[3][searchable]:true
columns[3][orderable]:true
columns[3][search][value]:
columns[3][search][regex]:false
columns[4][data]:4
columns[4][name]:
columns[4][searchable]:true
columns[4][orderable]:true
columns[4][search][value]:
columns[4][search][regex]:false
columns[5][data]:5
columns[5][name]:
columns[5][searchable]:true
columns[5][orderable]:true
columns[5][search][value]:
columns[5][search][regex]:false
columns[6][data]:6
columns[6][name]:
columns[6][searchable]:true
columns[6][orderable]:true
columns[6][search][value]:
columns[6][search][regex]:false
columns[7][data]:7
columns[7][name]:
columns[7][searchable]:true
columns[7][orderable]:true
columns[7][search][value]:
columns[7][search][regex]:false
columns[8][data]:8
columns[8][name]:
columns[8][searchable]:true
columns[8][orderable]:true
columns[8][search][value]:
columns[8][search][regex]:false
columns[9][data]:9
columns[9][name]:
columns[9][searchable]:true
columns[9][orderable]:false
columns[9][search][value]:
columns[9][search][regex]:false
order[0][column]:1
order[0][dir]:desc
order[1][column]:2
order[1][dir]:asc
start:0
length:10
search[value]:
search[regex]:false
_:1406133379280

server replies with the following error:
{ [MongoError: exception: the limit must be positive]
name: 'MongoError',
errmsg: 'exception: the limit must be positive',
code: 15958,
ok: 0 }

I cannot find much useful information online, can you help me with it?

P.S. my developing environment is

  1. nodejs 0.10.26
  2. express 4.6.1
  3. mongodb 1.4.7
  4. mongoose 3.8.13
  5. mongoose-datatable 0.2.8

Populate Two collection by reftype

My question about retrieve data by two collections with declare type and ref into schema.
example my schema:

  var controlunitSchema = new mongoose.Schema({
    osver: String,
    sensors: [{
      mac: { type: String, unique: true },
      alias: { type: String, default: 'SENSOR' },
      status: String,
      type: { type: mongoose.Schema.Types.ObjectId, ref: 'Sensor' }
    }]
  });

and Sensor Schema:

  var sensorSchema = new mongoose.Schema({
    id: { type: String, unique: true },
    description: String,
    timeCreated: { type: Date, default: Date.now },
    timeRegistered: { type: Date, default: Date.now }
  });

Now execute this code for fill my datatable:

  var options = {};
  options.populate = {
    ['sensors.type']
  };
  app.db.models.ControlUnit.dataTable(req.query, options, function (err, data) {
    if (req.xhr) {
      res.send(data);
    }
  });

but object sensors don't populate with id and description data.
If watch debug information in my console return:

...
sort: { osver: 'asc' },
  conditions: undefined,
  populate: [] }
Data: { sEcho: '1',
...

populate object don't exsist.
It's possible resolve this problem??
Thanks

Not working with __ttl document expiration via mongoose-ttl plugin

I have been using mongoose-datatable for a long time without any problems (great plugin, thanks!).

However, today I added a __ttl (document expiration) by using the mongoose-ttl plugin. I use this by first defining my schema as normal, and then invoking the plugin: schema.plugin(ttl, { ttl: '90d' });

The problem that started happening is that even though I get data back via dataTables, it says:
recordsTotal: 0, recordsFiltered: 0,

Is there a known issue when working with other plugins or the mongoose-ttl one in particular?

And here's a pastie of my debug output:

http://pastebin.com/eq3avEdh

Can't get array data

Hi,

What is the correct way of getting 'image' object below,

image: {
    public_id: String,
    version: String,
    alt: String,
    path: String
},

{data: 'image'} gaves error. returned json doesn't contain image field.
trace: Field Error: Unknown field path image ! error
Thanks

Nested object array search (Question)

Hi,

First of all thank you for the excellent plugin.

I have a problem when I'm working with nested objects with arrays. My model is:

...
loc: [{
    type: {
      type: String,
      default: 'Feature'
    },
    properties: {
      country: {
        type: String,
        default: 'nd'
      },
      status: {
        type: Number,
        default: '0'
      },
      modifiedAt: Date
    },
    geometry: {
      type: {
        type: String,
        default: 'Point'
      },
      coordinates: {
        type: [Number],
        index: '2dsphere'
      }
    }
  }]
...

and my goal is to retrieve, e.g. loc.properties.country, however I cannot see any info in my tables.
Please see my debug info:

Query: { draw: '1',
  columns:
   [ { data: 'loc.properties.country',
       name: '',
       searchable: 'true',
       orderable: 'true',
       search: [Object] } ],
  order: [ { column: '0', dir: 'asc' } ],
  start: '0',
  length: '10',
  search: { value: '', regex: 'false' },
  _: '1443568143064' }
Search Criteria builded: { options:
   { handlers: { field: { arrayPath: 'loc' } },
     conditions: { username: 'donald' } },
  pageStart: 0,
  pageSize: 10,
  nbColumns: 1,
  search: undefined,
  fields:
   [ Field {
       index: 0,
       model: 'users',
       path: 'loc.properties.country',
       searchable: true,
       search: undefined,
       sortable: true,
       sort: { dir: 'asc', precedence: 0 },
       selectable: true,
       type: 'String',
       ref: undefined,
       refType: undefined,
       arrayType: undefined,
       base: undefined,
       arrayPath: 'loc' } ],
  select: { 'loc.properties.country': 1 },
  sort: { 'loc.properties.country': 'asc' },
  conditions: { '$and': [ { username: 'donald' } ] },
  populate: [] }
Data: { draw: '1',
  recordsTotal: 1,
  recordsFiltered: 1,
  data:
   [ { _id: 5605528705fa0cd019316360,
       loc:
        [ { properties: { country: 'none' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } },
          { properties: { country: 'France' } } ] } ] }

How I can pass the right data to datatables?

Thanks.

Server Sort

Bonjour,

Je souhaite effectuer un sort sur ma collection globale.

Lorsque je met en place le sort, il s'effectue bien mais seulement sur les données retournées dans la page courante.

Je voudrais que le tri se fasse sur la collection globale et me retourne les valeurs en fonction.

Existe-t-il une syntaxe particulière pour faire cela ?

Merci d'avance.

server filter

Je ne sais pas pourquoi, mais les filtres coté serveur ne marche pas:

la manière dont je l'utilise (app/controllers/car_controller.js):
req.applicationModels.GestDep_Car.dataTable(req.query, {_user_id: req.user._id}, function(err, data)

on remarque que le _user_id est bon (il change pour chaque utilisateur), mais il n'est pas pris en compte dans la requete.

performance?

This works fine if the data is not large, however for now, I have around 450K rows in the mongo collection and the performance is significantly dropped. The first page takes like more than 10 secs to load. Is there any thing that we can do to improve it?

data table get a row data

before i use this plugin, i get a row data in datatables by

       jQuery('.js-dataTable-full').dataTable({
            "processing": true,
            "serverSide": true,
            "ajax": "/path/to/request/ajax",
            "createdRow": function(row, data, index) {
                  // todo something
            },
            "columnDefs": [{
                "targets": 0,
                "data": function ( row, type, val, meta ) {
                    return row;
                },
                "className": "text-center",
                "render": function ( row, type, full, meta ) {
                  // !!! i want to get the row data and do something at here
                  return '<a>' + row.name + '</a>';
                }
            }],
        });

when i use this plugin i get the error:
[Error: No valid field requested !]

i open the log and find the root cause:

in the file field.js i get a error Field Error: Unknown field path function ! error throw from here

if (!loadSchemaInfo(model, info, model.schema, path))
    throw Error(util.format("Unknown field path %s !", path));

the data field does not support function.

and then how can i get a full row data in datatables? plz give me some tips.

thanks.

Merge config

When configure is called, merge configuration instead of overriding it

Nested object search

I have a hidden column in datatables that I name it as 'tags'. The tags in my mongo is an array of strings:

[
    "tag1",
    "tag2"
]

I have a problem searching on the tags. I tried something like that without success:
https://datatables.net/examples/ajax/objects_subarrays.html

I tried to debug a bit the library but my time is limited. Is it a bug or is it something that I am missing from the documentation.

This is the datatables options:

    $(el).DataTable({
        'order': [
            [2, 'desc']
        ],
        'lengthMenu': [
            [25, 50, 100, -1],
            [25, 50, 100, 'All']
        ],
        'sAjaxSource': '/files/get/datatables',
        'bProcessing': true,
        'bServerSide': true,
        'columns': [{
            'mData': 'title'
        }, {
            'mData': 'description'
        }, {
            'mData': 'tags',
            'visible': false,
            'defaultContent': ''
        }]
    });

The search is working normally for the title and the description. Any ideas?

populate not working

Hi ,
First of all a big thanks for your great work.We are currently working on a project in which we use
Express 4
mongoose : 4.1.1
mongoose-datatable : 1.0.4

var company = new Schema({
company_name : String,
owner_first_name : String,
owner_last_name : String,
register_date : Date,
company_license_no : String,
company_addr1 : String,
company_addr2 : String,
company_addr3 : String,
company_pin : Number,
company_city : String,
company_state: String,
company_status : String,
company_value : String,
company_type : String,
parent_company_id : String,
createdby : String,
createddate : Date,
modifiedby : String,
modifieddate : Date
},{collection:'fms_company'});

var zone = new Schema({
zone_name : String,
zone_description : String,
createdby : String,
createddate : Date,
modifiedby : String,
modifieddate : Date
},{collection:'fms_zone'});

var zoneDeliveryCharge = new Schema({
company_id : [{ type: Schema.ObjectId, ref: 'Company' }],
zone_id : [{ type: Schema.ObjectId, ref: 'Zone' }],
delivery_charge : Number,
delivery_description : String,
createdby : String,
createddate : Date,
modifiedby : String,
modifieddate : Date
},{collection:'fms_zone_delivery_charge'});

I have created a DataTable for zoneDeliveryCharge Schema as follows,

var zoneDeliveryChargeListTable = $('#zoneDeliveryChargeList').DataTable( {
"processing" : true,
"serverSide" : true,
"responsive" : true,
"ajax" : {
"url": 'xxxxxx/list',
"data": function ( d ) {d.srch_company_id = $("#editCompanyID").val();}
},
"columns": [
{ "data": "zone_id.zone_name" },
{ "data": "delivery_charge" },
{ "data": "delivery_description" },
{
"class":"details-control text-center",
data: null,
defaultContent: '   ',
orderable: false
}],
"order": [[1, 'asc']]
});

When I try to access populated value,I get nothing,
var options = { conditions : { "company_id" : companyID} };
zoneDeliveryChargeModel.dataTable(req.query,options, function (err, data) {
res.send(data);
});

{"draw":"1","recordsTotal":2,"recordsFiltered":2,"data":[{"_id":"55d952af8cbfad3a05f8323a","delivery_description"
:"XXXXXXXX","delivery_charge":100,"zone_id":[]},{"_id":"55d964354df324fc05930182","delivery_description"
:"XXXXXXX","delivery_charge":200,"zone_id":[]}]}

I am getting zone_id always empty and don't finad any data getting populated into it.Is it something that I am doing wrong/missing. Or is it a bug ?

It would be better if you could provide a good example for populating refType....

Thanks & regards,
Hari

How to set conditions?

Hi,

How to set conditions?.....How i do this...., The documentation is little. I need set a many field type search and i have many fields through population, eg: { data: 'libreria.name',"defaultContent": "", width: '25%'}, i need search this field and others....Help.

Thanks.

How to populate an array of object property?

var ServiceModel = new Schema({
name: {type: String, required: true, unique: true},

plugins: [{type: Schema.ObjectId, ref: 'Plugin'}]

});

var PluginModel = new Schema({
name: {type: String, required: true}
);

client:
{ data : "plugins"},
{ data : "plugins.name"}

doesn't work.

mongoose virtuals

Looks like virtual selection isn't possible. Or maybe i'm doing something wrong.

Deep populate solution.

Hi!

First of all, thanks for this plugin and all the hard work you put into it.

I have a question regarding to the deep populate in mongoose. This is the client-side scenario:

"aoColumns": [
          {"mData": "_id"},
          {"mData": "product"},  //<==== Reference for autopopulate
          {"mData": "product.manufactor"}, //<=== Reference for deep autopopulate
]

Now when I request new data from the server, I have the following (default) implementation:

Item.dataTable(req.query, {}, function(err, data) {
    if (err) return next(err);
    res.send(data);
});

The problem is that the "product.manufactor" field only contains the ObjectID, not the deep population.

My solution to this problem is (using the async library):

Item.dataTable(req.query, {}, function(err, data) {
    if (err) return next(err);
    async.map(data.data, function(item, done){
      item.product.populate('manufactor', done);
    }, function(err){
      if(err) return next(err);
      res.send(data);
    });
});

Is there a better solution to this problem? (I'd prefer not to call this populate() on 10/25/50/100 items)

Greetings!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.