mogodb retriving last inserted document returns 0 retrieved documents

  mongodb, pymongo, python-3.x

I have this simple query in python pymongo to retrieve the last inserted document from a mongodb collection. Even though there is are documents in the collection, it returns 0 documents retrieved.

last_id_val = db_collection.media_m_service.find({}).sort([{"_id": -1}]).limit(1)

Is this the correct syntax? is there a better way to retrieve the last inserted document from any given collection collection.

Source: Python-3x Questions

LEAVE A COMMENT