Q: Nodejs mongoose- distinct with sort I am trying to get messages from unique chat rooms. With this query:
message.distinct( "chat_room",{ chat_room: {$regex : ".*" + email + ".*"}} ) 

I get only the list of unique chat rooms but I want to get all information from given collections (like time, id, etc.). What is more when I add:
.sort({ time: 1 })

I'm getting an error.

A: Instead of distinct you can use aggragate because sort and distinct cant be used at the same time it might throw an error that cannot sort with distinct
so what you need to do is
message.aggregate([{
           "$match":{ chat_room: {$regex : ".*" + email + ".*"}}
 },{
    $group:{_id:$chat_room}
    },{$sort:{
            'sort_field_name':-1
           }
       }]) 

