config/initializers/mongo_monkey_patches.rb
def from_db(klass, attributes = nil, selected_fields = nil)
      type = (attributes || {})[TYPE]
      if type.blank?
        klass.instantiate(attributes, selected_fields)
      else
        "MongoModel::#{type}".camelize.constantize.instantiate(attributes, selected_fields)
      end
    end
# Builds a new +Document+ from the supplied attributes loaded from the
# database.
#
# @example Build the document.
#   Mongoid::Factory.from_db(Person, { "name" => "Durran" })
#
# @param [ Class ] klass The class to instantiate from if _type is not present.
# @param [ Hash ] attributes The document attributes.
# @param [ Array ] selected_fields If instantiated from a criteria using
#   #only we give the document a list of the selected fields.
#
# @return [ Document ] The instantiated document.
Builds a new +Document+ from the supplied attributes loaded from the
database.
Builds a new + Document + from the supplied attributes loaded from the database .
def from_db(klass, attributes = nil, selected_fields = nil)
      type = (attributes || {})[TYPE]
      if type.blank?
        klass.instantiate(attributes, selected_fields)
      else
        "MongoModel::#{type}".camelize.constantize.instantiate(attributes, selected_fields)
      end
    end
def from_db ( klass , attributes = nil , selected_fields = nil ) type = ( attributes || { } ) [ TYPE ] if type . blank? klass . instantiate ( attributes , selected_fields ) else "MongoModel::#{type}" . camelize . constantize . instantiate ( attributes , selected_fields ) end end
Builds a new +Document+ from the supplied attributes loaded from the
database.
Builds a new + Document + from the supplied attributes loaded from the database .
The instantiated document.
The instantiated document .
 Document 
klass
attributes
selected_fields
selected_fields If instantiated from a criteria using #only we give the document a list of the selected fields.
selected_fields If instantiated from a criteria using #only we give the document a list of the selected fields .
example
Build the document. Mongoid::Factory.from_db(Person, { "name" => "Durran" })
Build the document . Mongoid :: Factory . from_db ( Person { " name " = > " Durran " } )