Git Product home page Git Product logo

mongoose-datatables's People

Contributors

archr avatar siedrix avatar umarmw avatar

Stargazers

 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  avatar  avatar

mongoose-datatables's Issues

Search not working if find contains $or condition, and search contains more than one field

For example, this options parameter does not work:

var options={
        find:{ $or:[ {'status':0}, {'status':2} ]},
        search: {
            value: "hello",
            fields: ['name','city']
        }
    };

It seems the searching fields conditions are added to the $or of the find, but I think this is not the expected behaviour. Instead, it should create an $and element containing two $or (one for the find and another one for the search fields), like this:

var options={
        find:{ 
            $and:[ 
                 {$or:[ {'status':0}, {'status':2} ] },
                 {$or:[ {'name':{'$regex': 'hello', '$options': 'i'}}, {'city':{'$regex': 'hello', '$options': 'i'}} ] }
            }
        }
    };

I think an "else" is needed in the "if" of the line 37 of mongoose-datatables.js

DeprecationWarning with Possible Fix Suggestion

DeprecationWarning:

collection.count is deprecated, and will be removed in a future version. Use collection.countDocuments or collection.estimatedDocumentCount

Suggested fix/solution

In mongoose-datatables.js line 82

Change:
.all([query.exec(), thisSchema.count(find)])

To:
.all([query.exec(), thisSchema.countDocuments(find)])

#16

Search and sort not working with multiple collection using datatable

Hi there,

I want to search from store and user collection but i am not getting data when search

I have three collection and below my schema

  1. Store Schema:
var mongoose = require('mongoose');
var dataTables = require('mongoose-datatables');
var storeSchema = new mongoose.Schema({
    store_name: String
});

storeSchema.plugin(dataTables);
module.exports = mongoose.model("Stores", storeSchema, 'tbl_stores');
  1. User Schema
var mongoose = require('mongoose');
var bcrypt = require('bcrypt-nodejs');
var dataTables = require('mongoose-datatables');

var userSchema = mongoose.Schema({
    firstname: {type: String},
    lastname: {type: String},
    email: {type: String, unique: true},
    password: {type: String}
});

userSchema.plugin(dataTables);
module.exports = mongoose.model('User', userSchema, 'users');
  1. Lookup Collection and i have set reference from above two tables
var mongoose = require('mongoose');
var dataTables = require('mongoose-datatables');
var userStoreSchema = new mongoose.Schema({
    store_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Stores'
    },
    user_id: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
});
userStoreSchema.plugin(dataTables);
module.exports = mongoose.model("storeUser", userStoreSchema, 'tbl_store_user');

This is my code for fetching data,when i am searching data from store name i am getting store data

var mongoose = require('mongoose');
var User = require('../models/dbuser');
var Stores = require('../models/dbstore');
var storeUser = require('../models/dbuserstore');

var order = req.body.order[0].column;
var dir = req.body.order[0].dir;
var col = req.body.columns[order].data;
jsObj = {};
jsObj[col] = dir;
storeUser.dataTables({
    limit: req.body.length,
    skip: req.body.start,
    populate:
            [ { path: 'store_id'}, { path: 'user_id'}],
    select: {_id: true, store_id: true, user_id: true},
    search: {
        value: req.body.search.value,
        field: ['store_id']
    },
    sort: jsObj
}).then(function (table) {
    res.json({
        data: table.data,
        recordsFiltered: table.total,
        recordsTotal: table.total
    });
});

Please help me out from this issue.
Thanks in advance

Question (Not Issue)

I've been going round in circles. Could you please tell me what I should use for URL: String?

ajax: {
url: '/', <<<<<<<<???
type: 'POST'
},
sorry i didnt know where elses to ask the question

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

Not work with Mongoose 4.10

The current version of Mongoose-datatables doesn't seem work with the lastest mongoose (4.1.0). Can you point the mismatch?

search when schema field is not String resolves in error

model:

var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    ObjectId = Schema.ObjectId;
var dataTables = require('mongoose-datatables');

var scoresSchema= new Schema({
    name: String,
    score: Number
});

scoresSchema.plugin(dataTables, {
    totalKey: 'recordsTotal',
    dataKey: 'data',
});
mongoose.model('ScoresSchema', scoresSchema);

express:

exports.scoresAjax = function (req, res) {
    var columns = [];
    columns = ['name', 'score'];
    var sort = {};
    if (req.body.order) {
        req.body.order.forEach(function (order) {
            var column = columns[order.column];
            sort[column] = order.dir === "desc" ? -1 : 1;
        })
    }
    Score.dataTables({
        limit: req.body.length,
        skip: req.body.start,
        search: {
            value: req.body.search.value,
            fields: ['name', 'score']
        },
        sort: sort,
        select: 'name score'
    }, function (err, table) {
if (err) {
return res.json([])
} else {
        var data = [];
        table.data.forEach(function (obj) {
            data.push([obj.name, obj.score]);
        })
        table.data = data;
        table.recordsFiltered = table.recordsTotal;
        res.json(table); // table.total, table.data 
    });
}
}

i get an error says

Error: Can't use $options with Number.
    at SchemaNumber.castForQuery (/home/bitnami/admin/node_modules/mongoose/lib/schema/number.js:278:13)
    at cast (/home/user/app/node_modules/mongoose/lib/cast.js:213:39)
    at cast (/home/user/app/node_modules/mongoose/lib/cast.js:36:18)
    at Query.cast (/home/user/app/node_modules/mongoose/lib/query.js:2741:10)
    at Query._count (/home/user/app/node_modules/mongoose/lib/query.js:1380:10)
    at /home/user/app/node_modules/kareem/index.js:239:8
    at /home/user/app/node_modules/kareem/index.js:18:7
    at _combinedTickCallback (internal/process/next_tick.js:67:7)
    at process._tickCallback (internal/process/next_tick.js:98:9)

and also when there is an error the callaback is called twice so i get an error:
Error: Can't set headers after they are sent.

aggregation in mongoose-datatables

Hi,
i am really confused as i want to use aggregate pipeline with mongoose-datatables.
This is my code
aModel.dataTables({
find: {isCertified: { $ne: null}},
limit: req.body.length,
skip: req.body.start,
search: {
value: req.body.search.value,
fields: ['status', 'user'],
andQuery: andQuery,
},
sort: {
[req.body.columns[req.body.order[0].column].data]: dir
},
populate: [
{ path: 'user' }
]
})
What i want is to do a left join
something like this
db.orders.aggregate([
{
$lookup:
{
from: "products",
localField: "products",
foreignField: "_id",
as: "productObjects"
}
}
])
my question is can i use aggregate with mongoose-datatables?
if yes, then how can i use it ?
if no, then are there any other method to do something like this/

Does not support full search() API

As much as I've seen, this tool does not support the full search() API:
(from https://datatables.net/reference/option/search)

search.caseInsensitive - Indicate is case-insensitive sorting should be performed.
search.regex - Treat the search as a regular expression or not
search.smart - Use DataTables' smart filtering or not
Additionally, to set an initial filter:
search.search - Set an initial global filter.

Searching not working if fields more than one

Hello @archr, I have problem when searching more than one field. This is my code:

User.dataTables({
    find: {
       $or: findRole,
     },
     limit: req.query.length,
     skip: req.query.start,
     search: {
          value: req.query.search.value,
          fields: ['email'],
     },
     sort: sort,
}, (err, table) => {
      table.recordsFiltered = table.recordsTotal;
      res.json(table);
});

Below the model code:

UserSchema.plugin(dataTables, {
    totalKey: 'recordsTotal',
    dataKey: 'data',
});

Thanks, sorry for my bad english

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.