Navigation

Mongoid 7.4

This page describes significant changes and improvements in Mongoid 7.4. The complete list of releases is available on GitHub and in JIRA; please consult GitHub releases for detailed release notes and JIRA for the complete list of issues fixed in each release, including bug fixes.

Ruby Version Support

As of version 7.4, Mongoid supports Ruby 2.5+. Support for Ruby 2.4 and earlier has been dropped.

=== Operator Matches Ruby Semantics

Breaking change: In Mongoid 7.4, the === operator works the same way as it does in Ruby, and is equivalent to calling is_a? on the right hand side with the left hand side as the argument:

ModelClass === instance

# equivalent to:
instance.is_a?(ModelClass)

Previously, === returned true for some cases when the equivalent Ruby === implementation returned false.

Mongoid 7.4 behavior:

class Band
  include Mongoid::Document
end

class CoverBand < Band
end

band = Band.new
cover_band = CoverBand.new

band === Band
# => false

cover_band === Band
# => false

Band === Band
# => false

CoverBand === Band
# => false

Mongoid 7.3 behavior:

band === Band
# => true

cover_band === Band
# => true

Band === Band
# => true

CoverBand === Band
# => true

The standard invocation of ===, that is having the class on the left and the instance on the right, works the same in Mongoid 7.4 as it did previously and matches the core Ruby behavior:

Band === band
# => true

Band === cover_band
# => true

BSON::ObjectId#as_json Implementation Removed

Mongoid in versions up to 7.3 used to provide a BSON::ObjectId#as_json implementation that was identical to the one provided by bson-ruby. Mongoid 7.4 removes its copy of this implementation.

This change has no immediate impact on applications - BSON::ObjectId#as_json behaves the same in Mongoid 7.4 as it did in Mongoid 7.3. However, if this implementation is changed in a future version of bson-ruby which is under consideration, Mongoid 7.4 and later will inherit the new implementation provided by bson-ruby while Mongoid 7.3 and earlier will continue with the implementation returning a hash of {"$oid" => "..."}.

Scoped Associations

Associations now support the :scope argument, yielding scoped associations.

distinct and pluck Respect Field Aliases In Embedded Documents

When distinct and pluck are used with aliased fields in embedded documents, the aliases are now expanded. Given the following definitions:

class Band
  include Mongoid::Document
  embeds_many :managers
end

class Manager
  include Mongoid::Document
  embedded_in :band

  field :name, as: :n
end

Mongoid 7.4 behavior:

# Expands out to "managers.name" in the query:
Band.distinct('managers.n')
Band.pluck('managers.n')

Mongoid 7.3 behavior:

# Sends "managers.n" without expanding the alias:
Band.distinct('managers.n')
Band.pluck('managers.n')

Note that the alias expansion for top-level fields has already been done by Mongoid 7.3.

count, sum, avg, min, max Ignore Sort If Not Limiting/Skipping

The count, sum, avg, min and max methods now omit the sort stage from the generated aggregation pipeline if no skip or limit is specified, because the results aren’t affected by the sort order. Example call that will now omit the sort stage and would potentially use an index where it wouldn’t before:

Band.desc(:name).count
←   Mongoid 7.5 Mongoid 7.3  →