blu*_*ren 11 regex mongodb regex-negation mongodb-query
我的 mongo 文档都包含一个名为templateName. 有迹象表明,包含值几个文件:a_SystemDefaultTemplate,b_SystemDefaultTemplate,c_SystemDefaultTemplate等。
我想要find那些 templateName 不以(或包含)结尾的文件SystemDefaultTemplate
我知道可以$not像这样使用操作符来完成:
db.collection.find({templateName: {$not: /.*SystemDefaultTemplate$/}})
Run Code Online (Sandbox Code Playgroud)
但是我如何使用正则表达式做同样的事情?
我已经尝试了以下但它似乎不起作用。
db.collection.find({templateName: {$regex: "^(.*SystemDefaultTemplate$)"}})
Run Code Online (Sandbox Code Playgroud)
尝试负面展望(意味着它不应该包含提到的短语)
db.collection.find({templateName: {$regex: "^(?!SystemDefaultTemplate$)"}})
Run Code Online (Sandbox Code Playgroud)
?!是消极的展望。这是来自http://rexegg.com/regex-disambiguation.html#lookarounds 的一些解释
"Negative Lookahead After the Match: \d+(?!\d| Dollar) Sample Match: 100 in 100 pesos 解释:\d+ 匹配 100,那么负向预测 (?!\d| Dollar) 断言在字符串,紧随其后的既不是数字也不是字符“?dollars”
Negative Lookahead Before the Match: (?!\d+ Dollar)\d+ Sample Match: 100 in 100 pesos 解释:负前瞻 (?!\d+ Dollar) 断言在字符串的当前位置,后面的不是数字 then字符“?美元”。如果断言成功,引擎将用 \d+ 匹配数字。”