MongoDB:如何使用python更新MongoDB中的String:String数据列表


x = mycol.insert_one({
"name":List_Name.keys(),
"time": List_Name.values()**strong text**``
})
tried this doesnt work`x = mycol.insert_one({
"name":List_Name.keys(),
"time": List_Name.values()
})

List_Name是一本字典。mongo新手上面写着-

bson.errors.InvalidDocument:无法对类型为<类"dict_keys">

keys()方法返回一个<class 'dict_keys'>对象,您需要将其强制转换为如下字符串:

separator = ", "
list_of_names = separator.join(List_Names.keys()) # keys separated by comma and space
x = mycol.insert_one({
"name":list_of_names,
})

最新更新