[英]how to to do “ab not dc” regex search in mongodb
我需要在具有条件“ ab not dc”的单个字段上进行正则表达式搜索。 下面的查询结果错误。 建议使用适当的查询
db.getCollection('collection').aggregate(
[
{$match:{search_description:{$regex: "ab", "$options": "i"}}}
,{$match:{search_description:{$not:{$regex: "dc", "$options": "i"}}}}
]
)
上面的查询结果“ errmsg”:“错误的查询:BadValue $不能有一个正则表达式”错误消息
由于MongoDB支持否定先行,因此您可以使用
db.getCollection('collection').aggregate(
[
{$match:{search_description:{$regex: "^(?!.*db).*ab.*$", "$options": "i"}}}
]
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.