Regex 从MongoDB正则表达式中排除字符串
我的文件看起来像这样Regex 从MongoDB正则表达式中排除字符串,regex,mongodb,Regex,Mongodb,我的文件看起来像这样 { "sId": "s1", "source": "September Challenge %2s", }, { "sId": "s2", "source": "September 2019 Challenge %2s" }, { "sId": "s3&qu
{
"sId": "s1",
"source": "September Challenge %2s",
},
{
"sId": "s2",
"source": "September 2019 Challenge %2s"
},
{
"sId": "s3",
"source": "September Challenge 2019"
}
我试图用regex2
在source
中搜索文档。
我得到了所有的3份文件。我想要下面的结果
{
"sId": "s2",
"source": "September 2019 Challenge %2s"
},
{
"sId": "s3",
"source": "September Challenge 2019"
}
第一个文档应该被忽略,因为它有%2s
,第二个和第三个文档应该被忽略,因为它包含2019
。
任何人都可以指导我如何实现上述目标。从您的评论来看,您似乎希望在
源代码
字段中找到带有2019
的文档,即使它包含%2
。您可以使用更具体的正则表达式来实现这一点
db.collection.find({ source: { $regex: "2019" } })
试试下面的正则表达式
db.getCollection('somecollection').find({field: {$regex: '(?<!%)<word>(?!=s)'}})
db.getCollection('somecollection').find({field:{$regex:'(?(!=s)})
为什么第二个文档不被忽略?在第二个文档2019
中有source
。谢谢:),但我不是专门针对2019
,它可能是2
或0
或9
或9月份的任何东西。有很多文档包含%2s
,我想忽略包含%2s
的文档,但是如果是第二个文档,如果我使用2
或9月
搜索,则不应忽略它。