0

I am trying to fetch data from MongoDB collection and return fetched data to calling function. Code is as follows.

client = pymongo.MongoClient("mongodb://localhost:27017")
db = client["DBName"]

def check(request):
    data = getData(2500, "2023-03-20")
    print(data)
    for d in data:
        print()
        print(d)
    return render(request, 'backtest.html')

def getData(value, somedate):
    collection = db[constantsCollection.NameData]
    query = {"$and": [{"value" : value}, {"datetime" : {"$regex" : somedate}}]}
    data = collection.find(query)
    return data

This code is giving error as AttributeError: 'Cursor' object has no attribute 'get'

Cursor is holding approx 10000 records.

I saw couple of answers but all are printing value in same function which works fine. (eg. Python MongoDB : 'Cursor' object has no attribute 'name') Looking for how to return value to calling function.

Thanks for help in advance.

Traceback :

Traceback (most recent call last): File "C:\Users\user\Envs\test\lib\site-packages\django\core\handlers\exception.py", line 56, in inner response = get_response(request) File "C:\Users\user\Envs\test\lib\site-packages\django\utils\deprecation.py", line 138, in call response = self.process_response(request, response) File "C:\Users\user\Envs\test\lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response if response.get("X-Frame-Options") is not None: AttributeError: 'Cursor' object has no attribute 'get'

4
  • There's nothing in the code you've shown that'd try to use a thing called .get(). Then again, you're not showing getocdatafrommongo. Commented May 10, 2023 at 18:15
  • You will also need to show the full traceback associated with the error, not just the error it's "giving". Commented May 10, 2023 at 18:15
  • Updated function name in code and added traceback Commented May 10, 2023 at 18:21
  • If that really is your traceback, then you're somehow returning the MongoDB cursor as a response, but that's also not what happens in the code you've posted. There's something off here that you're not showing us. Commented May 11, 2023 at 5:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.