MongoDB Bulkwrite which queries failed at match st

2019-08-17 10:35发布

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?

1条回答
看我几分像从前
2楼-- · 2019-08-17 11:34

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}}]}
查看更多
登录 后发表回答