简体   繁体   English

Mongo在两个日期之间的数组中查找时间戳

[英]Mongo find timestamps in a array, between two dates

I have documents with a array property timestamps as seen in the document below. 我有带有数组属性timestamps的文档,如下面的文档所示。 I wish to find all documents with timestamps between two dates. 我希望找到两个日期之间带有时间戳的所有文档。 My find query looks as follow: 我的查找查询如下所示:

    $subset = $db->col->find(
            array( 
                'timestamps'=> array(
                    '$elemMatch'=> array(
                        '$gte'=> new MongoDate(strtotime('2016-05-25 13:00:00')),
                        '$lte'=> new MongoDate(strtotime('2016-05-25 14:00:00'))
                    )
                )
            )
    );

But it does not return the expected documents. 但是它不返回预期的文件。 I tried to Google and search StackOverflow for answer but I cannot find anything explaining how to search for timestamps in a array, between two dates. 我试图用Google搜索SearchOverOverflow的答案,但是找不到任何解释如何在两个日期之间的数组中搜索时间戳的方法。

EDIT: 编辑:

It seems the problem lies with PHP because the same Python query works: 似乎问题在于PHP,因为相同的Python查询有效:

import datetime
f = '%Y-%m-%d %H:%M:%S'
c = db.col.find({
        'timestamps': {
            '$elemMatch': {
                '$gte': datetime.datetime.strptime('2016-05-25 13:00:00', f),
                '$lte': datetime.datetime.strptime('2016-05-25 14:00:00', f)
            }
        }
    })

Any help would be appreciated. 任何帮助,将不胜感激。

{
    "month": {
        "$date": 1464134400000
    }, 
    "timestamps": [
        {
            "$date": 1464181803000
        }, 
        {
            "$date": 1464182103000
        }, 
        {
            "$date": 1464182403000
        }, 
        {
            "$date": 1464183003000
        }, 
        {
            "$date": 1464183603000
        }, 
        {
            "$date": 1464184203000
        }, 
        {
            "$date": 1464184803000
        }, 
        {
            "$date": 1464185403000
        }, 
        {
            "$date": 1464186003000
        }
    ], 
    "status_history": [
        1, 
        1, 
        1, 
        0, 
        1, 
        1, 
        1, 
        0, 
        1
    ], 
    "_id": 12345
}

I had a look at find in array between dates in mongo and Get data between two dates mongo . 我看了一下在mongo中日期之间的数组中的find在两个日期mongo中获取数据

It was a timezone issue. 这是一个时区问题。 I created a DateTime and set the timezone to Etc/GMT and the expected data returned correctly. 我创建了一个DateTime并将时区设置为Etc/GMT并正确返回了预期的数据。

$gte = new MongoDate (new DateTime( '2016-05-25 13:00:00', new DateTimeZone( 'Etc/GMT' )));
$lte = new MongoDate (new DateTime( '2016-05-25 14:00:00', new DateTimeZone( 'Etc/GMT' )));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM