如何在MongoDB中使用“不喜欢"运算符

如何在MongoDB中使用“不喜欢

问题描述:

我可以使用pymongo来使用SQL Like运算符,

I can use the SQL Like Operator using pymongo,

db.test.find({'c':{'$regex':'ttt'}})

但是如何使用Not Like运算符?

我尝试过

db.test.find({'c':{'$not':{'$regex':'ttt'}})

但出现错误:

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

OperationFailure: $not cannot have a regex

来自 docs :

$ not运算符不支持$ regex的运算 操作员.而是使用//或在您的驱动程序界面中,使用 语言的正则表达式创建正则表达式的能力 对象.考虑以下使用模式匹配的示例 表达式//:

The $not operator does not support operations with the $regex operator. Instead use // or in your driver interfaces, use your language’s regular expression capability to create regular expression objects. Consider the following example which uses the pattern match expression //:

db.inventory.find( { item: { $not: /^p.*/ } } )

编辑(@idbentley):

EDIT (@idbentley):

{$regex: 'ttt'}通常与mongodb中的/ttt/等效,因此您的查询将变为db.test.find({c: {$not: /ttt/}}

{$regex: 'ttt'} is generally equivalent to /ttt/ in mongodb, so your query would become db.test.find({c: {$not: /ttt/}}

EDIT2(@KyungHoon Kim):

EDIT2 (@KyungHoon Kim):

在python中,这有效:'c':{'$not':re.compile('ttt')}

In python, this works: 'c':{'$not':re.compile('ttt')}