mongoose - Sort does not work on population query -
my code shown below:
var orderhistory = function(req, res) { order.find({ 'customer_id': req.body.user_id.trim() }).populate('foodtruck_id','-item_list').populate('items').populate('customer_id').sort({order_time: -1}) .exec(function(err, orders) { if (err) res.json({ status: '404', message: 'sorry, there no data' }); res.json({ status: '200', message: 'order list', data: orders }); }); };
order.js
var mongoose = require('mongoose'); var schema = mongoose.schema; var foodtruck = require('./foodtruck.js'); var payment = require('./payment.js'); var items = require('./items.js'); var order = new schema({ order_status:number, //0 place 1 accepted 2 cooked 3 cancelled foodtruck_id:{type:schema.types.objectid,ref: 'foodtruck'}, customer_id:{type: schema.types.objectid,ref: 'user'}, items:[{type : schema.types.objectid, ref : 'items'}], user_type:boolean, order_time:date, order_rating:{type:number,default:5.0}, order_issue_comments:string, order_special_instruction:string, order_total:number, order_location:string, order_coupon_code:string, payment_id:{type:schema.types.objectid,ref:'payment'} },{ versionkey: false }); module.exports = mongoose.model('order',order);
now want is, want query result in descending order of order_time criteria, somehow not work. how can achieve requirement?
Comments
Post a Comment