Ruby Mongo 驱动程序:如何查找日期间隔? [英] Ruby Mongo Driver: How To Look For Date Intervals?
本文介绍了Ruby Mongo 驱动程序:如何查找日期间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定一个初始日期和今天,搜索该日期之间的所有名称"的查询是什么?
Given a init date and today, what's the query to search for all "names" between that date?
谢谢
推荐答案
MongoMapper
您应该能够使用 MongoMapper 的查询运算符.假设您有一个带有created_on"日期的用户"模型,您可以使用它来获取名称.(我相信 MongoDB 使用 UTC 时间来存储所有日期/时间对象):
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 驱动程序
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
这篇关于Ruby Mongo 驱动程序:如何查找日期间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文