目前,這是我的代碼,用于在我的 mongodb 資料庫中查找最新檔案
async def _last_bump_time(self):
last_bump = bump_db.find().limit(1).sort('$natural', ASCENDING).limit(1)
last_bump = last_bump["date"]
return datetime.strptime(last_bump,"%S:%M:%H:%d:%m:%Y")
但它給了我一個錯誤,TypeError: index 'date' cannot be applied to Cursor instances
找到最反應檔案的最佳方法是什么,并且能夠從檔案中檢索資料而無需檔案有額外的引數?
' 我的檔案是這樣排序的,帶有三個引數
_id:6292a0d4cc2ef69845749028
id:736933464292589568
date:"16:23:20:28:05:2022"
uj5u.com熱心網友回復:
您正在使用findwhich 旨在檢索多個檔案。它回傳一個Cursor,它可以讓您一個接一個地瀏覽回傳的檔案。
您可以改為使用find_one,它將回傳單個檔案或None. find_one還支持傳遞sort引數,期望指定排序順序的(鍵,方向)對串列。放在一起,它可能看起來像這樣:
async def _last_bump_time(self):
last_bump = bump_db.find_one(sort=[('$natural', ASCENDING)])
# last_bump is None if your database is empty.
if last_bump:
return datetime.strptime(last_bump["date"],"%S:%M:%H:%d:%m:%Y")
return datetime.now() # Example return value in the case of an empty database. Feel free to update.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/482942.html
上一篇:PyMongo查詢|如何匯入引數
