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

$and/$or/$nor with empty array should be ignored from criteria (or return none, as appropriate)

    • Type: Icon: Improvement Improvement
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      Currently this happens:

       

      Customer.where({"$or"=>[]})
      #=> Mongo::Error::OperationFailure: [2:BadValue]: $and/$or/$nor must be a nonempty array (on 127.0.0.1:27017, modern retry, attempt 1)
      

       

      This is not very user friendly. Here is a very simple example which indicates how this makes application code messy:

       

      # given some dynamic criteria
      or_criteria = []
      or_criteria << {cond1: :foo} if x
      or_criteria << {cond2: :bar} if y
      
      # I end up writing these 5 lines...
      if criteria.present?
        Customer.where({"$or"=>or_criteria})
      else
        Customer.none
      end
      
      # ...I'd like to do just one line:
      Customer.where({"$or"=>or_criteria})

       

      We can consider the following:

      • empty $and --> returns none
      • empty $or --> returns none, unless chaining then its ignored
      • empty $nor --> ignored

      This ticket is similar to MONGOID-5030

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            shields@tablecheck.com Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: