Development configuration
One of the first things you'll encounter when starting your Rails app after upgrading to Mongoid 5 is an error about your database config being incorrect.
The fix is easy, just change sessions
to clients
:
Driver changes
In our codebase we "drop down to the driver" a lot to execute queries directly on moped/mongo-ruby-driver, instead of using Mongoid, e.g. to create collections for each account. Here you'll also need to change session
to client
.
Another change is that read
now expects a hash with a :mode
key, instead of the value directly:
Moped has an insert
method that accepts either a single document or an array of documents. The new mongo-ruby-driver comes with two separate methods, and you should pick one depending on the amount of documents you'd like to insert:
Lack of ordering
One of the biggest changes with the new driver is that documents are no longer ordered on _id
by default.
First and last no longer add an
_id
sort when no sorting options have been provided. In order to guarantee that a document is the first or last, it needs to now contain an explicit sort.
This means that anywhere where you rely on ordering (.first
, .last
) you need to explicitly order the query by _id
:
In order to make sure our code behaves as it used to, we created a concern that adds a default scope that orders by _id
:
FindAndModify
Find_and_modify
has been removed. Instead you now have 3 methods to chose from:
find_one_and_update
find_one_and_replace
(Convenience method, callsfind_one_and_update
)find_one_and_delete
ExpireAfterSeconds
One of the more obscure changes is the way a TTL index is created. We use TTL indexes to automatically purge customer data depending on their plan (e.g. after 7 days, or after a month).
The option on the index used to be called expire_after_seconds
, but has been renamed to expire_after
:
Staging/Production config changes
While in development we only needed to change sessions
to clients
, but our staging/production configs needed a lot more work:
username
has been renamed touser
and moved tooptions
password
has been moved tooptions
read
now expects a nested key namedmode
SSL
is no longer a nested hash, but is set underoptions
- The config requires a
replica_set
key if the setup is a replicaset
The upgrade documentation says that MongoDB 2.4 and 2.6 use :plain
auth, but we needed to remove the auth_mech
key all together for the setup to work.
Conclusion
Although this is quite an extensive list, we found the upgrade to be relatively painless and the new driver feels a lot more solid than the old Moped driver.