首先我需要"isFavourite": true
,然后"isReadByPro": false
,然后updatedAt:-1
。
我试了下面的方法,但不工作。
{ $sort: { updatedAt: -1 } },
{ $sort: { isReadByPro: 1 } },
{ $sort: { isFavourite: -1 } },
[
{
"_id": "628c8378c92d4b969cf4b53b",
"post": {
"title": "project 94608"
},
"isFavourite": true,
"isReadByPro": true,
"updatedAt": "2022-06-06T10:34:05.776Z"
},
{
"_id": "628f507192034120c7fc261a",
"post": {
"title": "your payment test 1"
},
"isFavourite": true,
"isReadByPro": true,
"updatedAt": "2022-05-27T10:39:16.493Z"
},
{
"_id": "628f50a792034120c7fc2681",
"post": {
"title": "your payment test 3"
},
"isFavourite": true,
"isReadByPro": true,
"updatedAt": "2022-05-27T08:42:48.403Z"
},
{
"_id": "628c76840ffe2cd088654d50",
"post": {
"title": "showcase 1"
},
"isFavourite": false,
"isReadByPro": true,
"updatedAt": "2022-05-24T06:10:38.364Z"
},
{
"_id": "628c780a0ffe2cd088654e21",
"post": {
"title": "project 1 test"
},
"isFavourite": false,
"isReadByPro": true,
"updatedAt": "2022-05-27T06:30:54.058Z"
},
{
"_id": "628c79cb5b4b0ee6020482df",
"post": {
"title": "project 2 test"
},
"isFavourite": false,
"isReadByPro": true,
"updatedAt": "2022-05-27T06:30:54.058Z"
}
]
为了按多个字段排序,它们应该在同一个排序对象上。它们的顺序是按优先顺序排列的。该数据首先按isFavourite
排序,然后,所有具有相同isFavourite
值的文档将在它们之间按isReadByPro
排序,以此类推。
{ $sort: {isFavourite: -1, isReadByPro: 1, updatedAt: -1} }