我可能很笨,但我有兩個由查詢結果回傳的游標物件users和locations
user_count = 0
location_count = 0
for user in users:
print(user) ## prints every object correctly
user_count = user_count 1
for location in locations:
print(location) ##only loops once on the first user
location_count = location_count 1
print(user_count, location_count) ## 1720 39
內回圈用戶似乎只執行一次,而外回圈按預期運行。
上面代碼的輸出:
#prints the first user
#prints all locations
#prints all remaining users
! missing locations
uj5u.com熱心網友回復:
如果locations是游標,它將是可迭代的,而不是串列、陣列或字典。它將被遍歷next,并且 MongoDB 游標不提供任何reset或rewind。
在為第一個用戶回圈后,游標耗盡,因此對于后續用戶,回圈立即結束。
您可以嘗試在回圈之前將游標讀入串列,例如
locations = list(db.collection.find())
您應該能夠多次遍歷該串列。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/338557.html
