Regex 如何使用';不像';MongoDB中的操作员

Regex 如何使用';不像';MongoDB中的操作员,regex,mongodb,mongodb-query,pymongo,sql-like,Regex,Mongodb,Mongodb Query,Pymongo,Sql Like,我可以使用pymongo使用SQLLike操作符 db.test.find({'c':{'$regex':'ttt'}}) 但是如何使用不象操作符呢 我试过了 db.test.find({'c':{'$not':{'$regex':'ttt'}}) 但有一个错误: OperationFailure:$not不能有正则表达式 从: $not运算符不支持使用$regex进行操作 操作人员在驱动程序界面中使用//或 语言的正则表达式创建正则表达式的能力 物体。考虑使用模式匹配的以下示例 表达式//

我可以使用
pymongo
使用SQL
Like
操作符

db.test.find({'c':{'$regex':'ttt'}})
但是如何使用
不象
操作符呢

我试过了

db.test.find({'c':{'$not':{'$regex':'ttt'}})
但有一个错误:

OperationFailure:$not不能有正则表达式

从:

$not运算符不支持使用$regex进行操作 操作人员在驱动程序界面中使用//或 语言的正则表达式创建正则表达式的能力 物体。考虑使用模式匹配的以下示例 表达式//:

编辑(@idbentley):

{$regex:'ttt'}
通常相当于mongodb中的
/ttt/
,因此您的查询将变成:

db.test.find({c: {$not: /ttt/}}
EDIT2(@KyungHoon Kim):

python中,下面是一个示例:

'c':{'$not':re.compile('ttt')}

您可以使用不包含单词的正则表达式。另外,对于不区分大小写的搜索,您可以使用
$options=>i

不包含
字符串

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
完全不区分大小写
string

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
字符串开头

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
字符串结尾

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
包含
字符串

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})
将此作为书签,并作为您可能需要的任何其他修改的参考。

要清楚,
{$regex:'ttt'}
通常相当于mongodb中的
/ttt/
,因此您的查询将变成
db.test.find({c:{$not:/ttt/}
@idbentley谢谢。我用您的澄清更新了我的答案。非常感谢。对于其他人,我使用了
'c':{$not':re.compile('ttt')}
。当然,您需要导入reGreat。我还为后代在答案中添加了您的注释。对于$ne和$regex,情况似乎是一样的。有人可以确认这一点,或者至少添加到它吗?我知道这有点晚了,但在MongoDB 4.2中,
$not
$regex
操作符的组合似乎对我有效。
db。collectionname.find({column:{$not:/^ttt$/}})
这很简单。
$not
$regex
可以从MongoDB 4.0.7开始一起使用。@Milan是的,这很简单,因为它是8年前作为答案发布的。您的评论没有帮助。