-1
這裏通過以相反的順序指數MongoDB中的子文檔進行排序是MongoDB中的子文件:如何在沒有更新的實際陣列
"comment_history" : [
"A",
"B",
"C",
]
我想是這樣的
"comment_history" : [
"C",
"B",
"A",
]
這裏通過以相反的順序指數MongoDB中的子文檔進行排序是MongoDB中的子文件:如何在沒有更新的實際陣列
"comment_history" : [
"A",
"B",
"C",
]
我想是這樣的
"comment_history" : [
"C",
"B",
"A",
]
隨着mongodb 3.4你可以使用「$ reverseArray」運算符
db.posts.aggregate([
{
$project:
{
title: 1,
comment_history: { $reverseArray: "$comment_history" }
}
}
])
你可以在'sort'中指定'-1'作爲降序或逆序排序 – Mekicha
[在查詢和項目的所有字段排序數組]可能重複(https://stackoverflow.com/questions/22888885/sort-array-in-query-and-project-all-fields) – felix
你試過把你的問題首先進入谷歌的標題?因爲['$ sort'](https://docs.mongodb.com/manual/reference/operator/update/sort/)的更新修飾符正好位於頂端,如果您的意圖是按照該順序「存儲」總是。如果你只是想在「結果」(沒有修改),然後一點搜索應該導致你[[$ reverseArray']](https://docs.mongodb.com/manual/reference/operator/aggregation/reverseArray /)。但是你的問題並沒有真正地說出你實際上意味着哪一個。 –