python - Why using integer as setting _id with pymongo doesn't work? -
this question has answer here:
i've tried larger data set in python , had issues, created small test set, in python pymongo:
from pymongo import mongoclient testcoll = mongoclient().tdb.tcoll data = {'foo': 'bar', 'baz': {1: {'a': 'b'}}, '_id': 'ab123456789'} testcoll.insert(data)
this returns
bson.errors.invaliddocument: documents must have string keys, key 1
replacing 1 in dictionary in baz 2 changes error key 2 accordingly
why this? missing ids in mongo?
i've submitted edit title of post considering misleading problem having. not trying update _id
field indicated rather python dictionary definition incompatible bson spec.
in line:
data = {'foo': 'bar', 'baz': {1: {'a': 'b'}}, '_id': 'ab123456789'}
you have numeric (integer) key value for, in mongo terms, document. deriving json, within bson spec not valid keys must strings required in specification.
bson.errors.invaliddocument: documents must have string keys, key 1
you need keep of keys in python code strings in order compliant.
data = {'foo': 'bar', 'baz': {'1': {'a': 'b'}}, '_id': 'ab123456789'}
changing representation of key string fixes problem.
as note, consider document structure there several disadvantages of using type of notation (if want access numerical index ) in mongodb documents collection on using array.
Comments
Post a Comment