简体   繁体   English

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

[英]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 不能有正则表达式

From the docs :文档

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 stringstring

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

Ends with stringstring

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.

相关问题 我可以使用正则表达式、Like 运算符和/或 Instr() 在较大的字符串中查找模式的索引吗? - Can I use regular expressions, the Like operator, and/or Instr() find the index of a pattern within a larger string? 如何在 Perl 替换运算符的替换端使用变量? - How can I use a variable in the replacement side of the Perl substitution operator? 使用MongoDB 3.0 Java驱动程序聚合框架时,如何在$ project运算符中使用$ substr表达式 - How do I use the $substr expression in $project operator when using MongoDB 3.0 Java Driver aggregation framework 如何通过 mongolab 在 mongodb 中使用正则表达式? - How can i use regex in mongodb over mongolab? 我可以在正则表达式中对or或运算符使用先行断言吗? - Can I use the lookahead assertion with an or operator in regex? 无法在mongodb中的$ in运算符中使用正则表达式 - unable to use regex in $in operator in mongodb MongoDB使用$ in运算符修改数组 - MongoDB use modified array with $in operator VB.NET-如何像RegEx一样使用此var? - VB.NET - How I can use this var like a RegEx? 在Perl中,如何使用正则表达式替换运算符替换子字符串中的非ASCII字符? - In Perl, how can I use the regex substitution operator to replace non-ASCII characters in a substring? 如何在Perl的s ///运算符中使用包含正则表达式特殊字符的模式? - How can I use a pattern that includes regex special characters in Perl's s/// operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM