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

search not working with populate data.

Open ultimateakash opened this issue 6 years ago • 1 comments

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

ultimateakash avatar Jun 25 '18 05:06 ultimateakash

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://[email protected]/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.

eherve avatar Mar 05 '19 09:03 eherve