【发布时间】:2015-04-30 17:21:30
【问题描述】:
我想验证 objectID 是一个有效的 mongoID 字符串。
目前我有:
import bson
try:
bson.objectid.ObjectId(id)
except:
pass # do something
我想让我的异常更具体,它看起来像 there is a solution,但 except bson.objectid.InvalidId 最终是 TypeError: id must be an instance of (str, unicode, ObjectId)。
好的,我试图进一步查看,found is_valid method 但bson.is_valid(1) 导致另一个错误TypeError: BSON data must be an instance of a subclass of str。
那么如何正确检查我的 objectID 是否有效?
【问题讨论】:
标签: python mongodb pymongo bson objectid