eherve / mongoose-datatable

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

Data from populate not sorting #31

Closed dragospanoiu closed 9 years ago

dragospanoiu commented 9 years ago

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?

eherve commented 9 years ago

Hello,

Thanks for using the module. Can you give me the models for me to test ?

Regards,

dragospanoiu commented 9 years ago

Hey,

I just changed a bit your demo project, I added a second model, looks like this:

// user model
usersSchema = new Schema(
{
  name : String
});
mongoose.model('User', usersSchema);
UModel = require('mongoose').model('User');

// reservation model
reservationsSchema = new Schema(
{
  name : String,
  startDate : String,
  endDate : {type: Schema.Types.ObjectId, ref: 'User'},
  confirmationCode : String
});
mongoose.model('reservations', reservationsSchema);
MyModel = require('mongoose').model('reservations');

And on the frontened I've changed the columns in the table like this:

"aoColumns" : [
    { "mData" : "name" },
    { "mData" : "startDate" },
    { "mData" : "endDate.name" },
    { "mData" : "confirmationCode" }
  ]

Whenever I try to sort by the endDate column is not working, although the query in populate says that is sorted by the name column in User model:

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

Am I doing something wrong?

eherve commented 9 years ago

Hi,

Sorry for the late reply ! The population in mongodb is done after the fetch of the root document. So it is not possible to sort by populated data. The sort in populate just sort the array of populated elements.

Regards