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

Use exists? or any? instead of count to determine existence of any document

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 8.0.1
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Fully Compatible

      Mongoid has code that wishes to know if a matching document is present in the database, for example in the uniqueness validator:

              add_error(document, attribute, value) if criteria.count > 1
      

      Usage of count is inefficient, this should use either any? or exists? depending on whether documents that only exist in the application need to be considered. In case of the uniqueness validator, exists? should be the correct method.

      The differences are described in https://www.mongodb.com/docs/mongoid/master/reference/associations/#any and https://www.mongodb.com/docs/mongoid/master/reference/associations/#exists.

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

              Created:
              Updated:
              Resolved: