'1TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
I am new at pymongo, but have a decent knowledge on mongoDB So basically I have a list of strings, then I converted it into a dictionary using key as integer, so as pymongo doesn't allow integer keys, I converted that dictionary to json using json.dumps() and then tried inserting into the DB. But its showing this error : TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
Sample code:
list = ["string1", "string2", "string3"]
dict1 = {}
for i in range(0, len(list)):
dict1[i] = list[i]
json1 = json.dumps(dict1)
connection = MongoClient("mongodb://localhost:27017/")
db = connection["test"]
collection1 = db["test"]
collection1.insert_one(json1)
Solution 1:[1]
You don't need to convert the dictionary to json, you can just insert the dictionary and that will work just fine.
collection1.insert_one(dict1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Belly Buster |
