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

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

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'

这里有什么问题?请用正确的语法重写代码,以便我能清楚地理解它。

你有两个问题;查询需要构建为一个字典(你的创建一个元组),并且查找的第一个参数只需要 query 而不是 {query}.

这应该更接近您的需要:

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)