Method: Mongoid::Contextual::Mongo#last

Defined in:
lib/mongoid/contextual/mongo.rb

#lastDocument

Note:

Mongoid previously added an _id sort when no sort parameters were provided explicitly by the user. This caused bad performance issues and was not expected, so #first/#last will no longer guarantee order if no sorting parameters are provided. For order guarantees - a sort must be explicitly provided.

Get the last document in the database for the criteria’s selector.

Examples:

Get the last document.

context.last

Returns:

Since:

  • 3.0.0



335
336
337
338
339
340
341
# File 'lib/mongoid/contextual/mongo.rb', line 335

def last
  try_cache(:last) do
    with_inverse_sorting do
      with_eager_loading(view.first)
    end
  end
end