page = open("npm.json", "r")
parsed = json.loads(page.read())
for i in parsed["dependencies"]:
collection.insert_one(i)
Hi, I am trying to read in a json file and populate my mongoDB with the rows called ependencies.it keeps giving me and error. I have tried insert, insert_one &insert_many to no avail.
The following is the error i get
File "database.py", line 43, in <module>
collection.insert_one(i)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
common.validate_is_document_type("document", document)
File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "database.py", line 43, in <module>
collection.insert_one(i)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
common.validate_is_document_type("document", document)
File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
The json file is a list of node dependencies. i ran the command npm list on a node project. Can anybody help Thanks in advance
It looks like your data in dependencies
is a dictionary
. This means when you iterate over it, you will get strings (which are the keys in this case).
An example would be like so:
my_entry = {'a': 1, 'b': 2}
for i in myentry:
print(i)
# will print 'a' then 'b', not 1 then 2
I'm assuming each dependency is a key, and you want to insert the value. So
# dictionary.values() will give you just the values
for v in parsed['dependencies'].values():
if isinstance(v, dict):
collection.insert_one(v)
else:
raise ValueError("object is not dictionary")
Mongo will only take json
or bson
entries, which are like dictionaries. When you pass keys
, you are trying to insert objects of type string
, which it doesn't support
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.