Mongoose schema includes this:
{
dueDate: { type: Date, required: false }
}
My goal is to fetch docs sorted by dueDate with the soonest date at the top, followed by the rest with no dueDate. My problem is that a due date is not required therefore docs with no dueDate get sorted above docs with a valid date.
This is how I sort it (node.js & mongoose.js):
query.sort({ dueDate: 'asc' });
I'm mainly looking for a built in way to do this; I've already implemented a hack by combining queries.
there is no built in way to do that. There is a ticket opened to make helpers available to do customized sort functions, it is planned but not schedulled: https://jira.mongodb.org/browse/SERVER-153
Check this thread: Is there a way to put all the nil value at the end when sorting with mongodb/mongoid?
Regards, Moacy