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

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

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

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