Home > Mobile >  what is the correct syntax here? getting type TypeError: unhashable type: 'dict
what is the correct syntax here? getting type TypeError: unhashable type: 'dict

Time:10-29

query={"colourCode" : "orange" },{"createdOn":{ "$gt" : my_datetime}},{"assignmentRef":{'$ne':None}}

cursor = collection.find({query},{'createdOn':1,'assignmentRef.name':1,'_id':0,'colourCode':1})


list_cur = list(cursor)

df = DataFrame(list_cur)
print(df)

Result

TypeError: unhashable type: 'dict'

what is the problem here? please rewrite the code with correct syntax, so that I clearly can understand it.

CodePudding user response:

You have two issues; the query needs to be constructed as a dictionary (yours creates a tuple), and the first parameter of the find needs to just be query not {query}.

This should be closer to what you need:

import datetime
from pandas import DataFrame
from pymongo import MongoClient

db = MongoClient()['mydatabase']
collection = db.mycollection
my_datetime = datetime.datetime.now()

query = {"colourCode": "orange", "createdOn": {"$gt": my_datetime}, "assignmentRef": {'$ne': None}}

cursor = collection.find(query, {'createdOn': 1, 'assignmentRef.name': 1, '_id': 0, 'colourCode': 1})

list_cur = list(cursor)

df = DataFrame(list_cur)
print(df)
  • Related