如何在 MongoDB 中使用“Not Like"运算符

如何在 MongoDB 中使用“Not Like

问题描述:

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

I can use the SQL Like Operator using pymongo,

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

但是我如何使用 Not Like 操作符?

But how can I use Not Like Operator?

我试过了

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

但出现错误:

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

OperationFailure: $not cannot have a regex

来自 文档:

$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/ ,所以你的查询会变成:

{$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中,下面的一个工作:

In python, below one works:

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