我正在尝试将defaultdict变量写入MongoDB中的文档。其他一切都很好,只是不是这一个属性,它奇怪!我正在设置一个名为'domains'的相当大的默认用户,之前已经工作了很多次。看看这个终端输出:
所以这是我的默认指示:
>>> type(domains)
<type 'collections.defaultdict'>
它非常大,约3mb:
>>> sys.getsizeof(domains)
3146008
以下是我们将其设置的文档:
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'res', u'date', u'_id']
让我们抓住该文件的ID:
>>> myID = db.AggregateResults.find_one({'date':'20110409'})['_id']
>>> myID
ObjectId('50870847f49a00509a000000')
太好了,让我们设置属性:
>>> db.AggregateResults.update({'_id':myID}, {"$set": {'domains':domains}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'res', u'date', u'_id']
EH?它没有保存?
嗯......什么都没有保存?
>>> db.AggregateResults.update({'_id':myID}, {"$set": {'myTest':'hello world'}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'myTest', u'res', u'date', u'_id']
好的......所以它可以保存很好的东西......也许是因为MongoDB不喜欢默认设置?我们试试吧:
>>> myDD = defaultdict(int)
>>> myDD['test'] = 1
>>> myDD
defaultdict(<type 'int'>, {'test': 1})
>>> db.AggregateResults.update({'_id':myID}, {"$set": {'myDD':myDD}})
>>> db.AggregateResults.find_one({'date':'20110409'}).keys()
[u'myTest', u'res', u'date', u'myDD', u'_id']
所以它可以保存默认值,只是没有这个??
太奇怪了!任何想法为什么??
编辑,安全=真:
>>> db.AggregateResults.update({'_id':myID}, {"$set": {'domains':domains}}, safe=True)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/collection.py", line 405, in update
_check_keys, self.__uuid_subtype), safe)
File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/connection.py", line 796, in _send_message
return self.__check_response_to_last_error(response)
File "/usr/lib64/python2.6/site-packages/pymongo-2.1.1_-py2.6-linux-x86_64.egg/pymongo/connection.py", line 746, in __check_response_to_last_error
raise OperationFailure(error["err"], error["code"])
pymongo.errors.OperationFailure: not okForStorage
This GoogleGroup discussion说这可能是因为钥匙上有全停,但是:
>>> [x for x in domains.keys() if '.' in x]
[]
答案 0 :(得分:4)
啊哈!找到了!
MongoDB中的密钥不仅没有&#39;。&#39;,他们也不能拥有&#39; $&#39;在他们中。
请参阅:
>>>[x for x in domains.keys() if '$' in x]
['$some_key_']
答案 1 :(得分:0)
我的猜测是你试图保存太大的文件。 MongoDB在其所有文档上最大大小为16MB。
尝试使用参数safe=True
运行update命令。这将以安全模式运行,该模式将指示数据库发回尝试插入的结果。