lib/mongoid/contextual/mongo.rb in mongoid-3.0.2 vs lib/mongoid/contextual/mongo.rb in mongoid-3.0.3
- old
+ new
@@ -28,11 +28,11 @@
#
# @return [ true, false ] If the context is empty.
#
# @since 3.0.0
def blank?
- count == 0
+ !exists?
end
alias :empty? :blank?
# Is the context cached?
#
@@ -149,11 +149,12 @@
#
# @return [ true, false ] If the count is more than zero.
#
# @since 3.0.0
def exists?
- count > 0
+ # Don't use count here since Mongo does not use counted b-tree indexes
+ !query.dup.select(_id: 1).limit(1).entries.first.nil?
end
# Run an explain on the criteria.
#
# @example Explain the criteria.
@@ -212,11 +213,11 @@
#
# @since 3.0.0
def initialize(criteria)
@criteria, @klass, @cache = criteria, criteria.klass, criteria.options[:cache]
@collection = klass.collection
- add_type_selection
+ criteria.send(:merge_type_selection)
@query = collection.find(criteria.selector)
apply_options
end
# Get the last document in the database for the criteria's selector.
@@ -322,24 +323,9 @@
query.update_all(attributes.__consolidate__)
end
alias :update_all :update
private
-
- # For models where inheritance is at play we need to add the type
- # selection.
- #
- # @example Add the type selection.
- # context.add_type_selection
- #
- # @return [ true, false ] If type selection was added.
- #
- # @since 3.0.0
- def add_type_selection
- if klass.hereditary? && !criteria.selector.keys.include?(:_type)
- criteria.selector.merge!(_type: { "$in" => klass._types })
- end
- end
# Apply the field limitations.
#
# @api private
#