Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4805

Criteria#not generates invalid queries when called with keyword arguments

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 7.1.0.rc0
    • Affects Version/s: None
    • Component/s: Query
    • None

      When `not` is given simple hashes, Mongoid trivially negates them with $not but the server does not support this syntax:

      irb(main):001:0> Band.not(name: 'test')
      => #<Mongoid::Criteria
        selector: {"name"=>{"$not"=>"test"}}
        options:  {}
        class:    Band
        embedded: false>
      
      irb(main):002:0> Band.not(name: 'test').to_a
      Traceback (most recent call last):
              2: from (irb):2
              1: from (irb):2:in `to_a'
      Mongo::Error::OperationFailure ($not needs a regex or a document (2) (on localhost:14420, attempt 1))
      

      Mongoid constructs working queries when not negates the next condition:

      irb(main):004:0> Band.not.where(name: 'test')
      => #<Mongoid::Criteria
        selector: {"name"=>{"$ne"=>"test"}}
        options:  {}
        class:    Band
        embedded: false>
      
      irb(main):005:0> Band.not.where(name: 'test').to_a
      => [#<Band _id: 5dc9f7d5ce4ef34893354323, name: "Sun Project", description: "Sun\nProject">]
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: