0

I have 2 schema. Named user and friends.

user schema is as follows:

var userSchema = mongoose.Schema({
    first_name: {type: String},
    last_name: {type: String},
    update_at: {type: Date}
})

Friends Schema is as follows:

var userfollowersSchema = mongoose.Schema({
    user_id: {type: String, ref: '"'User'"'},
    friends_id: {type: String, ref: 'User'}
})

I am populating friends id using below query. Mongoose

.populate('friends_id', '_id update_at', null, {sort: { 'update_at': -1 } })

I also tried:

.populate({
    path: 'friends_id',
    select: ('_id update_at'),
    options: {
        sort: '-following_id.update_at'
    }
})

But it is working with _id. I have got the result:

[{
    friends_id: {
        update_at: Fri May 13 2016 15:46:43 GMT+0530 (India Standard Time),
        count: 1,
        _id: 5735a23168264e700b17e6ee
    },
    _id: 5735a2bb68264e700b17e6f2
}, {
    friends_id: {
        update_at: Fri May 13 2016 15:47:31 GMT+0530 (India Standard Time),
        count: 0,
        _id: 5735a24168264e700b17e6ef
    },
    _id: 5735a2c068264e700b17e6f3
}, {
    friends_id: {
        update_at: Fri May 13 2016 18:47:13 GMT+0530 (India Standard Time),
        count: 2,
        _id: 5735a25868264e700b17e6f0
    },
    _id: 5735a2c568264e700b17e6f4
}]

Q : how to sort populate result with time? Mongoose version -

'"'mongoose'"': '"'^4.4.6'"',

0 Answers0