Mongoose query how to sort? again on top of a sort condition

the set of collections I now get through the mongoose query looks like this:

[{
    _id: "123456",
    sex: 0
},{
    _id: "222222",
    sex: 1
},{
    _id: "111111",
    sex: 1
},{
    _id: "333333",
    sex: 0
}]
:

[{
    _id: "123456",
    sex: 0
},{
    _id: "333333",
    sex: 0
},{
    _id: "111111",
    sex: 1
},{
    _id: "222222",
    sex: 1
}]

that is, under the condition that sex is ascending, _ id is ascending again.

use the sort (condition 1, condition 2) statement according to the answer given by the same question (https://codeshelper.com/q/10.), which does not return the result correctly, and the sort () of mongoose cannot be sorted by multiple conditions.

so I would like to ask if there is any way to achieve this effect when querying. Or is there any better algorithm that can be implemented?

Thank you


searched for some information, but it should not be written in this way. What I do is that when I take it from the database according to the first sort condition, after taking out the data, I write a sorting function to sort according to the second condition.


var arr=[{
    id: 123456,
    sex: 0
},{
    id: 222222,
    sex: 1
},{
    id: 111111,
    sex: 1
},{
    id: 333333,
    sex: 0
}];

arr.sort(function (a, b) {
   if(a.sex===b.sex){
       return a.id-b.id
   } else{
       return a.sex-b.sex
   }
});
Menu