Why using integer as _id parameter with pymongo does not work?

I tried this with a large dataset in python and had problems, so I created a small test suite, also in python with pymongo:

from pymongo import MongoClient
testColl = MongoClient().tDB.tColl
data = {'foo': 'bar', 'baz': {1: {'a': 'B'}}, '_id': 'AB123456789'}
testColl.insert(data)

It returns

bson.errors.InvalidDocument: documents must have only string keys, key was 1

replacing 1 in the dictionary in baz with 2 changes, the error to the key was 2, respectively

Why is this? Am I missing something at ids in mongo?

+4
source share
1 answer

I posted an edit on the title of your post, given that this is somewhat misleading to the problem you are facing. You did not try to update the field _idas indicated, but your Python Dictionary definition is incompatible with the BSON specification.

:

data = {'foo': 'bar', 'baz': {1: {'a': 'B'}}, '_id': 'AB123456789'}

( ) , Mongo, . JSON, BSON, , , .

bson.errors.InvalidDocument: documents must have only string keys, key was 1

Python , .

data = {'foo': 'bar', 'baz': {'1': {'a': 'B'}}, '_id': 'AB123456789'}

.

, ( ) MongoDB .

+8

Source: https://habr.com/ru/post/1525535/


All Articles