Execute the criteria or raise an error if no documents found.
@example Execute or raise
criteria.execute_or_raise(id)
@param [ Object ] args The arguments passed.
@raise [ Errors::DocumentNotFound ] If nothing returned.
@return [ Document, Array<Document> ] The document(s).
@since 2.0.0
# File lib/mongoid/criterion/findable.rb, line 17 def execute_or_raise(ids, multi) result = multiple_from_map_or_db(ids) check_for_missing_documents!(result, ids) multi ? result : result.first end
Find the matchind document(s) in the criteria for the provided ids.
@example Find by an id.
criteria.find(Moped::BSON::ObjectId.new)
@example Find by multiple ids.
criteria.find([ Moped::BSON::ObjectId.new, Moped::BSON::ObjectId.new ])
@param [ Array<Moped::BSON::ObjectId> ] args The ids to search for.
@return [ Array<Document>, Document ] The matching document(s).
@since 1.0.0
# File lib/mongoid/criterion/findable.rb, line 36 def find(*args) ids = args.__find_args__ raise_invalid if ids.any?(&:nil?) for_ids(ids).execute_or_raise(ids, args.multi_arged?) end
Adds a criterion to the Criteria
that specifies an id that
must be matched.
@example Add a single id criteria.
criteria.for_ids([ 1 ])
@example Add multiple id criteria.
criteria.for_ids([ 1, 2 ])
@param [ Array ] ids The array of ids.
@return [ Criteria ] The cloned criteria.
# File lib/mongoid/criterion/findable.rb, line 53 def for_ids(ids) ids = mongoize_ids(ids) if ids.size > 1 send(id_finder, { _id: { "$in" => ids }}) else send(id_finder, { _id: ids.first }) end end
Get the document from the identity map, and if not found hit the database.
@example Get the document from the map or criteria.
criteria.from_map_or_db
@return [ Document ] The found document.
@since 2.2.1
# File lib/mongoid/criterion/findable.rb, line 71 def from_map_or_db id = extract_id id = klass.fields["_id"].mongoize(id) if id doc = IdentityMap.get(klass, id || selector_with_type_selection) return nil if doc == {} doc && doc.matches?(selector) ? doc : first end
Get the documents from the identity map, and if not found hit the database.
@example Get the documents from the map or criteria.
criteria.multiple_from_map_or_db(ids)
@param [ ids ] The searched ids.
@return [ Array<Document> ] The found documents.
# File lib/mongoid/criterion/findable.rb, line 88 def multiple_from_map_or_db(ids) return entries if embedded? ids = mongoize_ids(ids) result = from_identity_map(ids) ids.empty? ? result : result + from_database(ids) end