0

Given a init date and today, what's the query to search for all "names" between that date?

Thank you

donald
  • 23,587
  • 42
  • 142
  • 223

1 Answers1

8

MongoMapper

You should be able to use MongoMapper's query operators. Suppose your have a "User" model with a "created_on" date, you could use this to get the names. (I believe MongoDB uses UTC Times to store all date/time objects):

initial_date = Time.utc(2011, 5, 1) # 2011-05-01 00:00:00 UTC
@users = User.where(:created_on => {:$gte => initial_date, :$lte => Time.now.utc})
@users.each do |user|
  puts user.name
end

Ruby Mongo Driver

initial_date = Time.utc(2011, 5, 1) # 2011-05-01 00:00:00 UTC
@conn = Mongo::Connection.new
@db = @conn['my_db']
@collection = @db['users']
@users = @collection.find(:created_on => {:$gte => initial_date, :$lte => Time.now.utc})
@users.each do |user|
  puts user['name']
end
Dylan Markow
  • 123,080
  • 26
  • 284
  • 201