Get the duplicate value on DuplicateKeyError

前端 未结 1 1230
栀梦
栀梦 2020-12-21 05:46

In pymongo, when a DuplicateKeyError caught, what\'s the proper way to find out the duplicate value behind the the exception?

Currently I do this

<
相关标签:
1条回答
  • 2020-12-21 06:15

    In dev version of pymongo (2.7) you can check with error_document property:

    try:
        db.coll.insert({name: 'some_value'})
    except pymongo.errors.DuplicateKeyError, e:
        print e.error_document
    

    As far as I know, in 2.6 and earlier versions, all info except error msg and code is discarded.

    0 讨论(0)
提交回复
热议问题