我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用pymongo.errors.InvalidDocument()。
def test_insert_check_keys(self, test_coll): bulk = test_coll.initialize_ordered_bulk_op() bulk.insert({'$dollar': 1}) with pytest.raises(InvalidDocument): await bulk.execute() bulk = test_coll.initialize_ordered_bulk_op() bulk.insert({'a.b': 1}) with pytest.raises(InvalidDocument): await bulk.execute()
def get_stream_writer(self, stream): """ Gets the database channel writer The mongoengine model checks whether a stream_id/datetime pair already exists in the DB (unique pairs) Should be overridden by users' personal channels - allows for non-mongo outputs. :param stream: The stream :return: The stream writer function """ def writer(document_collection): with switch_db(SummaryInstanceModel, 'hyperstream'): if isinstance(document_collection, StreamInstance): document_collection = [document_collection] for t, doc in document_collection: instance = SummaryInstanceModel( stream_id=stream.stream_id.as_dict(), datetime=t, value=doc) try: instance.save() except NotUniqueError as e: # Implies that this has already been written to the database # Raise an error if the value differs from that in the database logging.warn("Found duplicate document: {}".format(e.message)) existing = SummaryInstanceModel.objects(stream_id=stream.stream_id.as_dict(), datetime=t)[0] if existing.value != doc: raise e except (InvalidDocumentError, InvalidDocument) as e: # Something wrong with the document - log the error logging.error(e) return writer
def insertRagpickerDB(self, report): # Store the report try: self.__mongodbCollectionRagpicker.insert(report) except InvalidDocument as e: log.exception("Error InvalidDocument: %s", report) raise Exception("Error InvalidDocument: {0}".format(e)) except InvalidStringData: self.__mongodbCollectionRagpicker.insert(convertDirtyDict2ASCII(report)) #Count Ragpicker-Reports by file (and url)
def get_stream_writer(self, stream): """ Gets the database channel writer The mongoengine model checks whether a stream_id/datetime pair already exists in the DB (unique pairs) Should be overridden by users' personal channels - allows for non-mongo outputs. :param stream: The stream :return: The stream writer function """ def writer(document_collection): with switch_db(StreamInstanceModel, 'hyperstream'): if isinstance(document_collection, StreamInstance): document_collection = [document_collection] for t, doc in document_collection: instance = StreamInstanceModel( stream_id=stream.stream_id.as_dict(), datetime=t, value=doc) try: instance.save() except NotUniqueError as e: # Implies that this has already been written to the database # Raise an error if the value differs from that in the database logging.warn("Found duplicate document: {}".format(e.message)) existing = StreamInstanceModel.objects(stream_id=stream.stream_id.as_dict(), datetime=t)[0] if existing.value != doc: raise e except (InvalidDocumentError, InvalidDocument) as e: # Something wrong with the document - log the error logging.error(e) return writer