Using findOne in mongodb to get element with max id

后端 未结 5 1805
别那么骄傲
别那么骄傲 2020-12-01 01:18

I am trying to retrieve one element from a mongo collection, the one with the greatest _id field. I know this can be done by querying:

db.collection.find().s         


        
5条回答
  •  既然无缘
    2020-12-01 02:01

    import pymongo
    
    tonystark = pymongo.MongoClient("mongodb://localhost:27017/")
    
    mydb = tonystark["tonystark_db"]
    savings = mydb["customers"]
    
    x = savings.find().sort("_id")
    for s in x:
        print(s)
    

提交回复
热议问题