仅在特定时间查询 MongoDB 搜索 [英] Query MongoDB search only in specific hours

查看:22
本文介绍了仅在特定时间查询 MongoDB 搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我想按一天中的特定时间进行搜索,

i have a question, i want to search by specific hours of the day,

让我们开始吧:

我的数据架构如下所示:

My data schema sees like this:

{
 "symbol": "Orange",
 "timestamp": ISODate("2016-05-01T20:00:00.000Z"),
 "price": 10
}

我有很多条目,每分钟价格变化的价值.

I have a lot of entries, and the value of price change minute by minute.

如果我想搜索,假设一天中的小时数为:9 或 15,那么结果必须返回所有条目 9 到 9:59 和 15 到 15:59

If i want to search, let's say, all days when the hours of the day are: 9 or 15, so the result must return all the entries 9 to 9:59 and 15 to 15:59

我尝试做类似的事情:

 { $match: { timestamp: { $hour: { $in: [array_hours] } } }

但显然不行.

有什么想法吗?

推荐答案

我认为您可以使用 $hour, $day ... 来分解聚合中的日期.例如:

I think you can use $hour, $day ... to break down the date in aggregation. For example:

我的数据:

db.orders.find()
{ "_id" : ObjectId("5760bf21b05d6825e05fa23d"), "item" : "test1", 
          "create_at" : ISODate("2016-04-30T11:00:00Z") }
{ "_id" : ObjectId("5760bf30b05d6825e05fa23e"), "item" : "test2", 
          "create_at" : ISODate("2016-04-30T12:00:00Z") }
{ "_id" : ObjectId("5760bf37b05d6825e05fa23f"), "item" : "test3",  
          "create_at" : ISODate("2016-04-30T13:00:00Z") }

我的查询:

db.orders.aggregate([{$project:{hour:{$hour:"$create_at"}}}, 
                 {$match:{hour:{"$in":[11,12]}}}])
{ "_id" : ObjectId("5760bf21b05d6825e05fa23d"), "hour" : 11 }
{ "_id" : ObjectId("5760bf30b05d6825e05fa23e"), "hour" : 12 }

这里是文档

这篇关于仅在特定时间查询 MongoDB 搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆