Mongo游乐场链接:https://mongoplayground.net/p/BGtxQEBAvZh
我有一个数组字段“opcoes”,想通过日期
期望的结果是:
dt
对其进行排序。目前它是未排序的,例如:{
"_id" : "w900c9b5a-666f-4857-92da-0b9c273dca40",
"opcoes" : [
{
"dt" : ISODate("2021-11-16T11:17:55.386-03:00"),
"text" : "2"
},
{
"dt" : ISODate("2021-11-16T11:17:29.709-03:00"),
"text" : "3"
}
]
}
期望的结果是:
{
"_id" : "w900c9b5a-666f-4857-92da-0b9c273dca40",
"opcoes" : [
{
"dt" : ISODate("2021-11-16T11:17:29.709-03:00") ,
"text" : "3"
},
{
"dt" : , ISODate("2021-11-16T11:17:55.386-03:00")
"text" : "2"
}
]
}
你能帮我吗?我尝试使用$SortArray
,但它无法识别。我得到了“Invalid $project :: caused by :: Unknown expression $sortArray”。
我使用$push
创建了数组:
{
$group: {
_id: "$chat_key",
opcoes: {
$push: {
text: "$text",
dt: "$dt"
}
}
}
}
在$push
中是否有排序选项?
谢谢