为什么猫鼬抛出$strLenCP需要一个字符串参数,使用$exists后发现:null



我正试图通过属性bvn的字符串计数进行查询,由于我知道bvn的某些值可能为空,我添加了行

'bvn': { '$exist': true }

完整的线路是:

await this.find({$or: [
{'bvn': null},
{
'bvn': { '$exist': true },
'$expr': { '$gt': [ { '$strLenCP': '$bvn' }, 11 ] }
}
]
}).exec()

但我仍然遇到错误:

MongoError: $strLenCP requires a string argument, found: null

您当前正在测试字段"bvn";存在,但它可能是这样存在的:{bvn:null}问题是strLenCP需要一个字符串,但NULL不是字符串。Mongo实际上告诉你的是:

MongoError:$strLenCP需要一个字符串参数,找到:null

因此,在(!(执行strLenCP之前,您需要过滤掉管道中的NULL内容。

请这样尝试:

await this.find({$or: [
{'bvn': null},
{
'bvn': { '$exist': true },
'bvn' : { '$ne'  => null },
'$expr': { '$gt': [ { '$strLenCP': '$bvn' }, 11 ] }
}
]
}).exec()

最新更新