def get_oid(id_str): try: return bson.objectid.ObjectId(id_str) except bson.errors.InvalidId: return None