MongoDB Bulkwrite which queries failed at match step?

拈花ヽ惹草 提交于 2019-12-12 01:16:12

问题


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:


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}}]}


来源:https://stackoverflow.com/questions/53439102/mongodb-bulkwrite-which-queries-failed-at-match-step

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!