Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongodb bulk write error

Tags:

I'm executing bulk write

bulk = new_packets.initialize_ordered_bulk_op()

bulk.insert(packet)

output = bulk.execute()

and getting an error that I interpret to mean that packet is not a dict. However, I do know that it is a dict. What could be the problem?

Here is the error:

    BulkWriteError                            Traceback (most recent call last)     <ipython-input-311-93f16dce5714> in <module>()           2            3 bulk.insert(packet)     ----> 4 output = bulk.execute()      C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute(self, write_concern) 583         if write_concern and not isinstance(write_concern, dict): 584             raise TypeError('write_concern must be an instance of dict')     --> 585         return self.__bulk.execute(write_concern)      C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute(self, write_concern) 429             self.execute_no_results(generator) 430         elif client.max_wire_version > 1:     --> 431             return self.execute_command(generator, write_concern) 432         else: 433             return self.execute_legacy(generator, write_concern)      C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute_command(self, generator, write_concern) 296                 full_result['writeErrors'].sort( 297                     key=lambda error: error['index'])     --> 298             raise BulkWriteError(full_result) 299         return full_result 300       BulkWriteError: batch op errors occurred 
like image 843
David Makovoz Avatar asked May 20 '15 16:05

David Makovoz


People also ask

What is bulk write error in MongoDB?

A bulk operation that resulted in a BulkWriteError may have written some of the documents to the database. If the bulk write was unordered, writes may have also continued past the write that produced a BulkWriteError. Exception raised if there are write errors upon executing a bulk operation.

How do I bulk write in MongoDB?

In MongoDB, the Bulk. insert() method is used to perform insert operations in bulk. Or in other words, the Bulk. insert() method is used to insert multiple documents in one go.

What is BulkWriteError?

Represents an error for an item included in a bulk write operation, e.g. a duplicate key error.

How do I update MongoDB bulk records?

We can update multiple documents from the collection by using the bulk update method in MongoDB. 2) InitializeOrderedBulkOp – This method is used with the bulk update method to update multiple documents from the collection. We need to call this method at the time of using the bulk update method in MongoDB.


2 Answers

It can be many reasons...
the best is that you try...catch... the exception and check in the errors

from pymongo.errors import BulkWriteError try:     bulk.execute() except BulkWriteError as bwe:     print(bwe.details)     #you can also take this component and do more analysis     #werrors = bwe.details['writeErrors']     raise 
like image 185
Samer Aamar Avatar answered Oct 29 '22 06:10

Samer Aamar


Ok, the problem was that i was assigning _id explicitly and it turns out that the string was larger than 12-byte limit, my bad.

like image 21
David Makovoz Avatar answered Oct 29 '22 04:10

David Makovoz