原搜索因需求更改需要增加聚合操作,写了好久搞不定,求救~
原搜索:
await db.collection('xx').where(`type=0&&(status=1 ${isLogin ? "|| user_id = 'uid' ": ''}) && ${new RegExp('xxx')}.test(text)`)
当我尝试修改成下面这样时:
await db.collection('xx').aggregate().match({
type: 0,
text: new RegExp('xxx'),
status: $.cond({
if: isLogin ? $.eq(['$user_id', uid]) : false,
then:dbcmd.or($.eq(['$status', 0]),$.eq(['$status', 1]))
else:$.eq(['$status', 1])
})
}).lookup()
会报这个错误:
InternalServerError errCode: InternalServerError | errMsg: Command failed with error 2 (BadValue): 'unknown operator: $cond' on server 0.0.0.0:0. The full response is { "operationTime" : { "$timestamp" : { "t" : 0, "i" : 2 } }, "ok" : 0.0, "errmsg" : "unknown operator: $cond", "code" : 2, "codeName" : "BadValue", "$clusterTime" : { "clusterTime" : { "$timestamp" : { "t" : 0, "i" : 2 } }, "signature" : { "hash" : { "$binary" : "0", "$type" : "00" }, "keyId" : { "$numberLong" : "0" } } } }(env: Windows,mp,1.05.2204250; lib: 2.24.3)
上面报错敏感数据用0代替,好像是在match中不支持cond?还是我的写法有问题?
后面我将写法换成:
await db.collection('xx').aggregate().match({
dbcmd.expr($.and(
$.eq(['$type', 0]),
isLogin ? $.or([$.eq(['$status', 1]),
$.eq(['$user_id', uid])])
: $.eq(['$article_status', 1]),
// todo regex
))
}).lookup()
我发现不知道怎么写这个正则...查文档没有发现$.regex这样的操作符
AlphaDex (作者)
那请问这种情况我需要怎么解决?
2022-06-14 14:59
DCloud_uniCloud_WYQ
回复 AlphaDex: 只是模糊匹配的话可以考虑用indexOf实现 https://uniapp.dcloud.net.cn/uniCloud/cf-database-aggregate-operator.html#indexofbytes, 但是需要结合自己的业务场景来,如果你的集合数据量很大,这个查询可能会比较慢
2022-06-15 16:31
AlphaDex (作者)
回复 DCloud_uniCloud_WYQ:好的谢谢,已经用其他方式实现
2022-06-16 23:15