def insert(self, doc): """Insert the document into the collection. Parameters ---------- doc : dict A document to insert into the MongoDB collection. Returns ------- str The hexadecimal id of the inserted document. Notes ------ If a document with the same key already exists, catches the :class:`~pymongo.errors.DuplicateKeyError` and returns the id of the original document. """ try: inserted_result = self._collection.insert_one(doc) obj_id = inserted_result.inserted_id except pymongo.errors.DuplicateKeyError as error: errmsg = error.details['errmsg'] key_val = parserutils.get_dup_key_val(errmsg) dup = self._collection.find_one(key_val) obj_id = dup['_id'] return str(obj_id)
def test_simple_index(self): """ Tests the get_dup_key_val function for a simple index """ errmsg = 'E11000 duplicate key error collection: ' + \ 'test_database_1482164012.test_docs index: _id_ dup key: ' + \ '{ : 593849043618549760 }' actual = parserutils.get_dup_key_val(errmsg) expected = {'_id': 593849043618549760} self.assertEqual(actual, expected)
def test_compound_index(self): """ Tests the get_dup_key_val function for a compound index """ errmsg = 'insertDocument :: caused by :: 11000 E11000 ' + \ 'duplicate key error collection: cyphon.posts index: _platform_1__raw_data_1 ' + \ 'dup key: { : "twitter", : ObjectId(\'5543769ef861c942838c7ee9\') }' actual = parserutils.get_dup_key_val(errmsg) expected = { '_platform': 'twitter', '_raw_data': ObjectId('5543769ef861c942838c7ee9') } self.assertEqual(actual, expected)
def test_nested_compound_index(self): """ Tests the get_dup_key_val function for a compound index on nested fields. """ errmsg = 'E11000 duplicate key error collection: test_database.test_posts index: ' + \ '_raw_data.backend_1__raw_data.collection_1__raw_data.doc_id_1 ' + \ 'dup key: { : null, : null, : null }' actual = parserutils.get_dup_key_val(errmsg) expected = { '_raw_data.backend': None, '_raw_data.collection': None, '_raw_data.doc_id': None } self.assertEqual(actual, expected)