Implement enumerable methods on top level models (any?, many?, one?, etc)

XMLWordPrintableJSON

    • Type: New Feature
    • Resolution: Fixed
    • Priority: Minor - P4
    • 9.1.0
    • Affects Version/s: None
    • Component/s: None
    • 🔵 Done
    • Fully Compatible
    • Ruby Drivers
    • Needed
    • Hide

      1. What would you like to communicate to the user about this feature?

      User.any?, User.one?, and User.many? where added. User.any? returns true if there are any documents in the collection. User.one? returns true if there is exactly one document in the collection. User.many? returns true if there are more than one documents in the database.

      Show
      1. What would you like to communicate to the user about this feature? User.any?, User.one?, and User.many? where added. User.any? returns true if there are any documents in the collection. User.one? returns true if there is exactly one document in the collection. User.many? returns true if there are more than one documents in the database.
    • None
    • None
    • None
    • None
    • None
    • None

      Rubocop recently introduced a new cop that converts User.count > 0 into User.any?, but Mongoid does not support these methods at the top level. I propose delegating these methods to scoped, which does support them.

      User.any? => User.scoped.any?
      User.one? => User.scoped.one?
      User.many? => User.scoped.many?
      

      Additionally, these methods pull all records, then perform ruby's enumerable methods on the resulting array, which is not performant. I'd also propose making them use mongodb count function if the records have no already been loaded into memory.

            Assignee:
            Kyle Burgess
            Reporter:
            Kieran Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: