使用 Mongoid 和 Ruby 查询最近 30 天的日期范围?
如何使用 Mongoid 和 Ruby 查询日期范围(例如从现在起的最后 30 天)?
我需要最终得到一个如下所示的数组或哈希:
{
15 => 300,
14 => 23,
13 => 23
...
30 => 20 # Goes over into previous month
28 => 2
}
我当前使用 DateTime 实例以及 unix 时间戳整数字段存储每个文档。
上述哈希中的键是日期,值是这些日期的所有销售额的总和。
有什么想法吗?
How do I go about querying a date range (of say the last 30 days from now) with Mongoid and Ruby?
I need to end up with an array or hash like the following:
{
15 => 300,
14 => 23,
13 => 23
...
30 => 20 # Goes over into previous month
28 => 2
}
I am currently storing each document with a DateTime instance as well as a unix timestamp Integer field.
The keys in the above hash are the days and the values are the sum of all sales for those days.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个更简单的方法:
调整时间范围以适应。
There's a simpler way:
Adjust time range to suit.
以下是如何在 rubyland 中完成这一切:
查询始终为 30,则会导致键冲突。
这将创建一个带有“月-日”键的哈希,原因是有些月份的天数少于 30 天,如果 不同的范围,更改查询:
将
created_at
更改为日期时间字段的名称。Here's how to do it all in rubyland:
This will create a hash with "month-day" keys, reason is that some months have fewer than 30 days and will result in a key collision if the query is always 30.
If you want a different range, change the query:
Change
created_at
to whatever the name of your datetime field is.您还可以使用
Between 方法编写查询,例如:
You can also write the query by using the
between
method like:如果您忘记在模型中添加时间戳怎么办? :(
没问题!只需使用 BSON:ObjectId
获取过去 30 天内的销售额。
这很可能是最快的查询
需要日期范围?
由于
id
字段是索引, 上个月的销售额。当然这个例子假设 Rails 日期助手...
What if you forgot to put timestamps in your model? :(
No problem! Just use the timestamp in the BSON:ObjectId
Get Sales in the last 30 days.
Since the
id
field is index, this may very well be the fastest query.Need a date range? Cake.
Get Sales from last month.
Of course this example assumes Rails date helpers...
例如,您可以通过使用仅发出相关条目(日期值大于您给出的任何条件的条目)的映射函数对集合进行
map_reduce
调用来实现此目的。尝试这样的事情:
这可能有用。
You can achieve this for example by doing a
map_reduce
call over your collection with a map function only emitting the relevant entries (the ones whose date value is greater than whatever condition you give it).Try something like this:
That might work.