1
在一個小博客應用程序中,我想根據日期降序排列評論。最新的評論將是最高的。Pymongo排序評論降序
一個典型的後看起來是這樣的:
{
"_id" : 15,
"title" : "Soup making techniques",
"content" : "In this tutorial we'd like to share best soup making practices.",
"updatedate" : ISODate("2017-10-19T21:13:19.193Z"),
"comments" : [
{
"content" : "This is my first comment.",
"_id" : 25,
"date" : ISODate("2017-10-19T21:13:31.328Z")
},
{
"content" : "Another comment.",
"_id" : 26,
"date" : ISODate("2017-10-19T21:29:36.536Z")
}
]
}
而且對蟒蛇側的相關代碼如下所示;
post = document.find_one({'_id': int(number)}, sort=[("comments.date", -1)])
result = document.find_one({ '_id' : int(number) , "comments": { '$exists': True, '$ne': False } })
comments = []
commentlist = []
if result:
commentlist = post['comments']
print ("All comments", commentlist)
for comment in commentlist:
comments.append({'commentid' : comment['_id'], 'date' : comment['date'], 'content' : comment['content']})