-1

I am using the pymongo with python to perform CRUD operations to a mongodb database. And I need to get the last 10 records, Here's my code:

from pymongo import MongoClient
# get the values of the environment variables 
MONGODB_HOSTNAME = os.environ['MONGO_URI']
MONGODB_USERNAME = os.environ['MONGODB_USERNAME']
MONGODB_PASSWORD = os.environ['MONGODB_PASSWORD']
MONGODB_DATABASE = os.environ['MONGODB_LOGS_DATABASE']
MONGODB_COLLECTION_NAME = os.environ['MONGODB_COLLECTION_NAME']

client = MongoClient("mongodb://"+MONGODB_USERNAME+":"+MONGODB_PASSWORD+"@mongodb/admin")

db = client[MONGODB_DATABASE]

collection = db[MONGODB_COLLECTION_NAME]
cursor = collection.find().sort({'natural',1}).limit(10)

But I get the following error:

TypeError: if no direction is specified, key_or_list must be an instance of list

Can someone help me what I am doing wrong here?

Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105

1 Answers1

0

I was able to fix it with the following code:

cursor = collection.find().sort("_id", -1).limit(3)
Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105