如何在MongoDB中使用“Not Like”操作符

时间:2022-01-01 11:58:47

I used the SQL 'Like' Operator using pymongo,

我使用了SQL 'Like'操作符pymongo,

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

But how can I use 'Not Like' Operator?

但是我如何使用“不喜欢”操作符呢?

I tried

我试着

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

2 个解决方案

#1


91  

From the docs:

从文档:

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 //:

$not操作符不支持与$regex操作符的操作。相反,在驱动程序接口中使用//或使用语言的正则表达式功能来创建正则表达式对象。考虑下面的例子,使用模式匹配表达式//:

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

db.inventory。找到({项目:{ $不是:/ ^ p。* / } })

EDIT (@idbentley):

编辑(@idbentley):

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

{$regex: 'ttt'}在mongodb中通常等同于/ttt/,因此您的查询将成为db.test。找到({ c:{ $不是:/ ttt / } }

EDIT2 (@KyungHoon Kim):

EDIT2(@KyungHoon金):

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

在python中,如此:“c”:{“不是美元”:re.compile(ttt)}

#2


31  

You can do with regex which does not contain word. Also you can use $options => i for case insensitive search

您可以使用不包含word的regex。还可以使用$options => i进行大小写不敏感搜索

Doesn't Contains string

不包含字符串

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

Exact case insensitive string

完全不分大小写字符串

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})

Start with string

从字符串开始

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

End with string

结尾字符串

db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})

Contains string

包含字符串

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Keep this as a bookmark, and a reference for any other alterations you may need. http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

把它作为书签,作为你可能需要的任何其他修改的参考。http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

#1


91  

From the docs:

从文档:

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 //:

$not操作符不支持与$regex操作符的操作。相反,在驱动程序接口中使用//或使用语言的正则表达式功能来创建正则表达式对象。考虑下面的例子,使用模式匹配表达式//:

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

db.inventory。找到({项目:{ $不是:/ ^ p。* / } })

EDIT (@idbentley):

编辑(@idbentley):

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

{$regex: 'ttt'}在mongodb中通常等同于/ttt/,因此您的查询将成为db.test。找到({ c:{ $不是:/ ttt / } }

EDIT2 (@KyungHoon Kim):

EDIT2(@KyungHoon金):

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

在python中,如此:“c”:{“不是美元”:re.compile(ttt)}

#2


31  

You can do with regex which does not contain word. Also you can use $options => i for case insensitive search

您可以使用不包含word的regex。还可以使用$options => i进行大小写不敏感搜索

Doesn't Contains string

不包含字符串

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

Exact case insensitive string

完全不分大小写字符串

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})

Start with string

从字符串开始

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

End with string

结尾字符串

db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})

Contains string

包含字符串

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Keep this as a bookmark, and a reference for any other alterations you may need. http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/

把它作为书签,作为你可能需要的任何其他修改的参考。http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/