MongoDB : How do I update String :List of String data in MongoDB using python?

Viewed 22
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 is a dictionary. New to mongo It says-

bson.errors.InvalidDocument: cannot encode object: dict_keys(['Pushpraj']), of type: <class 'dict_keys'>

1 Answers

The keys() method returns a <class 'dict_keys'> object, you need to cast it to a string like so:

separator = ", "
list_of_names = separator.join(List_Names.keys()) # keys separated by comma and space

x = mycol.insert_one({
    "name":list_of_names,
})
Related