我是MongoDB的新手,我正在使用this guide创建我的第一个数据库。
你已经有很多SO文章,为什么你不能使用“。”或{$ 3}},this和this等密钥中的“$”。在大多数情况下,人们确实试图在其中存储具有句点的键并且试图找到绕过它们的方法。我不是试图存储任何带有句点的键,我让Mongo创建它自己的默认ID ......但我仍然得到错误。我做错了什么?
这是我创建对象的时候:
article = {"news_source": news_source,
"url": url,
"article_text":article_text,
"article_summary": article_summary,
"article_keywords": article_keywords,
"article_title": article_title,
"article_metadata": article_metadata}
这是我第一次尝试将其插入数据库的地方:
article_id = fox_collections.insert_one(article).inserted_id
这是我得到的丑陋的错误信息:
---------------------------------------------------------------------------
InvalidDocument Traceback (most recent call last)
<ipython-input-163-160806aafe79> in <module>()
----> 1 article_id = fox_collections.insert_one(article).inserted_id
//anaconda/lib/python2.7/site-packages/pymongo/collection.pyc in insert_one(self, document, bypass_document_validation)
623 return InsertOneResult(
624 self._insert(sock_info, document,
--> 625 bypass_doc_val=bypass_document_validation),
626 self.write_concern.acknowledged)
627
//anaconda/lib/python2.7/site-packages/pymongo/collection.pyc in _insert(self, sock_info, docs, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val)
528 return self._insert_one(
529 sock_info, docs, ordered,
--> 530 check_keys, manipulate, write_concern, op_id, bypass_doc_val)
531
532 ids = []
//anaconda/lib/python2.7/site-packages/pymongo/collection.pyc in _insert_one(self, sock_info, doc, ordered, check_keys, manipulate, write_concern, op_id, bypass_doc_val)
510 command,
511 codec_options=self.codec_options,
--> 512 check_keys=check_keys)
513 _check_write_command_response([(0, result)])
514 else:
//anaconda/lib/python2.7/site-packages/pymongo/pool.pyc in command(self, dbname, spec, slave_ok, read_preference, codec_options, check, allowable_errors, check_keys, read_concern)
216 # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
217 except BaseException as error:
--> 218 self._raise_connection_failure(error)
219
220 def send_message(self, message, max_doc_size):
//anaconda/lib/python2.7/site-packages/pymongo/pool.pyc in _raise_connection_failure(self, error)
344 _raise_connection_failure(self.address, error)
345 else:
--> 346 raise error
347
348 def __eq__(self, other):
InvalidDocument: key 'dcterms.modified' must not contain '.'
答案 0 :(得分:0)
基本上 article_metadata 对象是其他元素的字典,其中包含句点....
所以,这个故事的教训 - 注意格式化为字典或jsons的嵌套对象,它们可能有自己的一组键,需要符合mongo的规则......