如何在 MongoDB 中使用“Not Like"运算符 [英] How can I use 'Not Like' operator in MongoDB

查看:80
本文介绍了如何在 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')}

这篇关于如何在 MongoDB 中使用“Not Like"运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆