在perl中查询ISODate时间 [英] Query ISODate time in perl

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

问题描述

我有一个应用程序,需要能够通过mongoshell在以下查询中查询日期范围,但我无法终生通过perl MongoDB驱动程序重现它

I have an application that I need to be able to query date ranges in the following query works via the mongoshell but I cannot for the life of me reproduce it via the perl MongoDB driver

db.matches.find({ last_seen: { $gte: new Date("2014-05-15T00:00:00.000Z")}});

我的初始Perl查询看起来像(我知道很天真)

my intial perl query looked like (I know naive)

$matches->find({ last_seen => { '$gte' => "new Date(\"2014-05-15T00:00:00.000Z\")"}});

数据的一个例子是

{
    "_id" : ObjectId("5365e47c183aa8df9dee7558"),
    "count" : NumberLong(21),
    "matches" : [ 
        "Team 2", 
        "Team 2", 
        "Team 2", 
        "Team 1", 
        "Team 1", 
        "Team 1", 
        "Team 2", 
        "Team 1", 
        "Team 1", 
        "Team 2", 
        "Team 1", 
        "Team 2", 
        "Team 1", 
        "Team 1", 
        "Team 1"
    ],
    "player1" : "Team 1",
    "player2" : "Team 2",
    "last_seen" : ISODate("2014-05-17T08:16:05.000Z")
}

推荐答案

与大多数语言实现一样,您在语言实现中使用本机日期"类型,而不是字符串. MongoDB将日期(ISODate)存储为日期类型将被序列化为的实际BSON重新表达(实际上是时间戳).

As with most language implementations you use the native "date" types to your language implementation rather than strings. MongoDB stores the date (ISODate) as an actual BSON repraesentation ( actually a timestamp ) that the date type will be serialized into.

用于Perl的 MongoDB 驱动程序支持 DateTime :: Tiny 用于序列化和反序列化:

The MongoDB driver for Perl supports DateTime and DateTime::Tiny for serialization and deserialization:

my $cursor = $matches->find({
    last_seen => { 
       '$gte' => DateTime->new( year => 2014, month => 5, day => 15 )
    }
}

或者,您实际上将获得用于比较的日期对象.

Or however you are going to actually obtain the date object you use to compare.

请参见数据类型部分.

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

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