python - TypeError: 'str' object does not support item assignment with PyMongo (MongoDB API) -
i'm trying follow mongodb - quick guide , i'm getting following error:
alexus@mbp:~ $ python python 2.7.10 (default, oct 23 2015, 18:05:06) [gcc 4.2.1 compatible apple llvm 7.0.0 (clang-700.0.59.5)] on darwin type "help", "copyright", "credits" or "license" more information. >>> import json >>> pymongo import mongoclient >>> client = mongoclient('mongodb://192.168.99.100:32768/') >>> db = client['test'] >>> collection = db['c'] >>> >>> obj = 'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc:{tag1:value1}' >>> print obj adc83b19e793491b1c6ea0fd8b46cd9f32e592fc:{tag1:value1} >>> json.dumps(obj, separators=(',', ':'), sort_keys=true) '"adc83b19e793491b1c6ea0fd8b46cd9f32e592fc:{tag1:value1}"' >>> db.c.insert(json.dumps(obj, separators=(',', ':'), sort_keys=true)) traceback (most recent call last): file "<stdin>", line 1, in <module> file "/library/python/2.7/site-packages/pymongo/collection.py", line 2199, in insert check_keys, manipulate, write_concern) file "/library/python/2.7/site-packages/pymongo/collection.py", line 578, in _insert gen(), check_keys, self.codec_options, bwc) file "/library/python/2.7/site-packages/pymongo/collection.py", line 546, in gen doc['_id'] = objectid() typeerror: 'str' object not support item assignment >>>
what doing wrong?
the "insert" call expects valid object, not string. did mean that:
db.c.insert({'adc83b19e793491b1c6ea0fd8b46cd9f32e592fc':{'tag1':'value1'}})
Comments
Post a Comment