mongoid/origin — pull request #45 by potatosalad (2012-09-13 19:55:21+00:00)
Title: $not criterion negation for regular expressions

Here's a little excerpt MongoDB's [documentation](http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-Metaoperator%3A%7B%7B%24not%7D%7D):

```javascript
// The $not meta operator can be used to negate the check performed by a standard operator. For example:

db.customers.find( { name : { $not : /acme.*corp/i } } );

db.things.find( { a : { $not : { $mod : [ 10 , 1 ] } } } );
```

The [operators reference](http://docs.mongodb.org/manual/reference/operators/#_S_not) in the manual has some more information on using ```$not``` with regular expressions.

This pull request is my attempt to add a ```:field.not``` criterion while maintaining the current ```query.not``` functionality. For regular expression values only, it allows the issue #44 syntax to work.

```ruby
class User
include Mongoid::Document
field :name
end

# previously, this would not negate the regexp

User.not.where(name: /Andrew/).selector
=> { "name" => /Andrew/ }

# I changed the String and Symbol extensions to detect a regexp and consider it negatable

User.not.where(name: /Andrew/).selector
=> { "name" => { "$not" => /Andrew/ } }

# the following syntax also works

User.where(:name.not => /Andrew/).selector
=> { "name" => { "$not" => /Andrew/ } }

User.not(name: /Andrew/).selector
=> { "name" => { "$not" => /Andrew/ } }

# and finally, I have it check so double negatives don't happen

User.not.where(:name.not => /Andrew/).selector
=> { "name" => { "$not" => /Andrew/ } }
```

There may be a better way to accomplish including the ```$not``` for regular expressions, but I hope that the code and specs are helpful. Thanks!