Mongodb:基于ISODate格式的时间查询 [英] Mongodb : Query based on time in ISODate format

查看:10637
本文介绍了Mongodb:基于ISODate格式的时间查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Mongodb数据库中的示例文档如下所示:

Suppose the sample documents in Mongodb database are as follows:

 { "date" : ISODate("2015-11-09T05:58:19.474Z") }
 { "date" : ISODate("2014-10-25T07:30:00.241Z") }
 { "date" : ISODate("2015-11-30T15:24:00.251Z") }
 { "date" : ISODate("2012-01-10T18:36:00.101Z") }

预期:

 { "date" : ISODate("2015-11-09T05:58:19.474Z") }
 { "date" : ISODate("2014-10-25T07:30:00.241Z") }

我有兴趣查找日期字段中的时间在04:00至08:00之间的文档,无论日期,月份和年份如何。间接查询必须与日期字段中的任何YYYY-MM-DDT相匹配。

Iam interested in finding the documents whose time in "date" field is between 04:00 and 08:00 irrespective of day,month and year. Indirectly query has to match any "YYYY-MM-DDT" in date field.

我的方法是在节点之后的推定的日期期间查询所有文档,然后对于与查询相匹配的每个文档,将文档的日期字段与yyyy-MM-DDT+required_time进行比较(YYYY-MM-DD从每个文档的日期字段复制)通过转换为时刻进行比较()并获取月份,日期和年份)使用moment.js模块。

My approach would be, query all the documents within presumed duration of dates from node and then for each document that matched the query, compare the "date" field of document with "yyyy-MM-DDT"+"required_time" ("YYYY-MM-DD is copied from each document's "date field" to compare by converting into moment() and get month,date and year") using moment.js module.

有没有什么方法来查询直接获得相同的结果?

Is there any way to query to directly get the same results?

注意:我正在使用nodejs连接到mongodb

Note: I am using nodejs to connect to mongodb

推荐答案

一种方法是使用 聚合框架 ,尤其是 $ redact 操作符,它根据值剥离内容的文档流在文件及其子文件中。根据布尔表达式的结果,可以从流中修剪文档,在其子文档也被检查之后包含在流中,或者刚刚被传递到流中。 $ refact 是从流中轻松删除敏感信息。

One approach is to use the aggregation framework, in particular the $redact operator which strips the document stream of content based on values within the document and its sub-documents. Depending on the result of a boolean expression, a document can be pruned from the stream, be included in the stream after its sub-documents have also been checked, or just passed complete into the stream. The idea behind $redact is to make the removal of sensitive information from the stream easy.

在您的情况下,条件表达式使用 $ cond 运算符和 $和 布尔运算符表示时间范围与比较运算符之间的逻辑AND $ gt $ lt 。使用 $ hour date运算符将日期字段的小时作为0到23之间的数字返回。因此,您的最终聚合看起来像这样:

In your case, the criteria expression uses the $cond operator and the $and boolean operator to express the logical AND between the time ranges with the comparison operators $gt and $lt. Use the $hour date operator to return the hour for the date field as a number between 0 and 23. Thus your final aggregation looks like this:

db.collection.aggregate([
    {
        "$redact": {
            "$cond": {
                "if": { 
                    "$and": [
                        { "$gt": [ {"$hour": "$date"}, 4] },
                        { "$lt": [ {"$hour": "$date"}, 8] }
                    ]                 
                },
                "then": "$$KEEP",
                "else": "$$PRUNE"
            }
        }        
    }
])

示例输出:

/* 0 */
{
    "result" : [ 
        {
            "_id" : ObjectId("56404450472fe25cc6b85886"),
            "date" : ISODate("2015-11-09T05:58:19.474Z")
        }, 
        {
            "_id" : ObjectId("56404450472fe25cc6b85887"),
            "date" : ISODate("2014-10-25T07:30:00.241Z")
        }
    ],
    "ok" : 1
}

这篇关于Mongodb:基于ISODate格式的时间查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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