【发布时间】:2020-04-13 15:46:52
【问题描述】:
我正在尝试按日期对 cmets 进行排序,但我无法正确操作。
{
"_id": "5defc10b8e753623b4ad0adf",
"title": "bla bla",
"owner:"idToPopulate",
"comments": [
{
"_id": "5dfc2185e62103121cfc0f18",
"reply": "1",
"replyDate": "2019-12-10T16:00:11.228Z"
},
{
"_id": "5dfc218be62103121cfc0f19",
"reply": "2",
"replyDate": "2019-13-10T16:00:11.228Z"
}
]
}
这是我尝试过的方式(结果必须是,按上次日期对 cmets 进行排序)
.sort({"comments.replyDate":1})
【问题讨论】:
-
你为什么不试试 '1' 而不是 '-1' 呢?我认为
-1表示升序(对于 1 然后 2 正确) -
嗨 @CeliusStingher 我的错误(输入错误),但它也不起作用
-
您必须使用聚合来对数组中的子文档进行排序 - 按子文档的字段排序。在这篇文章中查看聚合的答案:How to sort sub-documents in the array field?。