lib/mongoid/contextual/mongo.rb in mongoid-3.0.0 vs lib/mongoid/contextual/mongo.rb in mongoid-3.0.1
- old
+ new
@@ -194,10 +194,11 @@
#
# @return [ Document ] The first document.
#
# @since 3.0.0
def first
+ apply_id_sorting
with_eager_loading(query.first)
end
alias :one :first
# Create the new Mongo context. This delegates operations to the
@@ -401,20 +402,35 @@
if spec = criteria.options[:hint]
query.hint(spec)
end
end
+ # Apply an ascending id sort for use with #first queries, only if no
+ # other sorting is provided.
+ #
+ # @api private
+ #
+ # @example Apply the id sorting params.
+ # context.apply_dd_sorting
+ #
+ # @since 3.0.0
+ def apply_id_sorting
+ unless criteria.options.has_key?(:sort)
+ query.sort(_id: 1)
+ end
+ end
+
# Map the inverse sort symbols to the correct MongoDB values.
#
# @example Apply the inverse sorting params.
# context.apply_inverse_sorting
#
# @since 3.0.0
def apply_inverse_sorting
if spec = criteria.options[:sort]
query.sort(Hash[spec.map{|k, v| [k, -1*v]}])
else
- query.sort({_id: -1})
+ query.sort(_id: -1)
end
end
# Is the cache able to be added to?
#