[英]How can I use 'Not Like' operator in MongoDB
I can use the SQL Like
Operator using pymongo
,我可以使用
pymongo
使用 SQL Like
运算符,
db.test.find({'c':{'$regex':'ttt'}})
But how can I use Not Like
Operator?但是我怎样才能使用
Not Like
Operator?
I tried我试过
db.test.find({'c':{'$not':{'$regex':'ttt'}})
but got error:但有错误:
OperationFailure: $not cannot have a regex
OperationFailure:$not 不能有正则表达式
The $not operator does not support operations with the $regex operator.
$not 运算符不支持使用 $regex 运算符的操作。 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.*/ } } )
EDIT (@idbentley):编辑(@idbentley):
{$regex: 'ttt'}
is generally equivalent to /ttt/
in mongodb, so your query would become: {$regex: 'ttt'}
通常相当于 mongodb 中的/ttt/
,因此您的查询将变为:
db.test.find({c: {$not: /ttt/}}
EDIT2 (@KyungHoon Kim): EDIT2 (@KyungHoon Kim):
In python , below one works:在python 中,下面一个工作:
'c':{'$not':re.compile('ttt')}
You can do with regex which does not contain a word.您可以使用不包含单词的正则表达式。 Also, you can use
$options => i
for case of insensitive search.此外,您可以使用
$options => i
进行不敏感搜索。
Doesn't Contain string
不包含
string
db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})
Exact case insensitive string
不区分大小写的
string
db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})
Starts with string
以
string
db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})
Ends with string
以
string
db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})
Contains string
包含
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.