eherve / mongoose-datatable

Server side dataTable request support for mongoose
MIT License
42 stars 29 forks source link

search not working with populate data. #53

Closed ultimateakash closed 1 year ago

ultimateakash commented 6 years ago

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

eherve commented 5 years ago

Hi, sorry for the late reply !

Search on populated data will be possible in the v2.0.0 of this module (actual master branche but not on npm yet).

In previous version it was not possible because the populated data were fetch after the main query to fetch data. In v2.0.0 the populated data are retrieved in the main query by using aggregation $lookup. You can test it by importing the v2.0.0 in your project by using : npm install --save git+ssh://git@github.com/eherve/mongoose-datatable.git

Be careful, mongodb is not relational database so it is better to include data you need in your root document. The $lookup will make you request slow if you have a lot of data.

Let me know if this helps.