Skip to content

Instantly share code, notes, and snippets.

View brianhempel's full-sized avatar

Brian Hempel brianhempel

View GitHub Profile
criteria.merge!( :_keywords => { :$in => words } )
search_result = collection.map_reduce(search_map(words), search_reduce, {:query => criteria, :finalize => search_finalize})
def search_reduce
"function( key , values ){return { model: values[0]};}"
end
def search_finalize
"function( key , values ){return values.model;}"
criteria.merge!( :_keywords => { :$in => words } )
search_result = collection.map_reduce(search_map(words), search_reduce, :query => criteria)
def search_reduce
"function( key , values ){return values[0];}"
end
def search_map(words)
#convert words into Regex OR
# single collection inheritance
# all will be stored in the Person collection in the db
class Person
include MongoMapper::Document
end
class Registree < Person
key :visitor, Boolean
scope :vistors, :visitor => true
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor
class Person
include MongoMapper::Document
end
class Registree < Person
key :in_wels, Boolean
key :in_vienna, Boolean
end
class Visitor < Registree
@brianhempel
brianhempel / polymorphic_playing.rb
Created January 14, 2011 18:34
MongoMapper Polymorphism with Single Collection Inheritance (SCI)
class Commentable
include MongoMapper::Document
many :comments, :as => :commentable
end
class Article < Commentable
end
class Product < Commentable
end
@brianhempel
brianhempel / global_stat.rb
Created January 21, 2011 18:44
mongomapper custom types
class GlobalStats
include MongoMapper::Document
key :attempts, Attempts
end
class Attempts
attr_accessor :total, :hours
def self.to_mongo(attempts)
{
@brianhempel
brianhempel / mongo_config.rb
Created February 21, 2011 23:53
Don't need much to get MM running
# ENV['MONGOHQ_URL'] = 'mongodb://localhost/sushi'
MongoMapper.database = "mes-#{Rails.env}"
# if ENV['MONGOHQ_URL']
# MongoMapper.config = {RAILS_ENV => {'uri' => ENV['MONGOHQ_URL']}}
# MongoMapper.connect(RAILS_ENV)
# end
if Rails.env == "development"
@brianhempel
brianhempel / mm_indexes.rb
Created February 22, 2011 00:11
Indexing in MongoMapper
def build_indexes!
ensure_index [["parent_id", Mongo::ASCENDING]]
ensure_index [["place_type", Mongo::ASCENDING], ["name", Mongo::ASCENDING]]
ensure_index [["ancestor_ids", Mongo::ASCENDING], ["place_type", Mongo::ASCENDING]]
end
def ensure_index(index_keys)
puts "Ensuring index on #{index_keys.map{|i| i[0]}.join(', ')}..."
collection.ensure_index index_keys
end