0

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?

Sudhanshu Gaur
  • 7,486
  • 9
  • 47
  • 94
  • 1
    In short, No. You only get counts. These are not "errors" so there is deemed to be no need report them or at which index of the batch a `0` was returned. A very common usage pattern is to actually send several update requests where you really only expect "one" of those to actually match and do anything. So it's not as if it's a high demand feature. If you think you need this, then you would not be sending requests in bulk, and simply sending individual requests and inspecting the results. This is the price of admission. – Neil Lunn Nov 23 '18 at 06:24

1 Answers1

1

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}}]}
Bajal
  • 5,487
  • 3
  • 20
  • 25