I am doing bulkwrite operation in MongoDB to update multiple documents at a time.
Now Is there any way by which I can know which sequence number of my queries match step failed.
Because in returned document I am getting nModified
, nMatched
which tells how many match failed, but not which query sequence number got failed?
You can use BulkWriteResult.writeErrors. It is available in both ordered and unordered mode of operation. Specifically, the "op" field will tell you the document that failed.
Here is a sample output from pymongo reference:
{'nInserted': 0,
'nMatched': 1,
'nModified': 1,
'nRemoved': 0,
'nUpserted': 0,
'upserted': [],
'writeConcernErrors': [],
'writeErrors': [{u'code': 11000,
u'errmsg': u'...E11000...duplicate key error...',
u'index': 1,
u'op': {'_id': 4}}]}