Reputation: 764
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")}});
my intial perl query looked like (I know naive)
$matches->find({ last_seen => { '$gte' => "new Date(\"2014-05-15T00:00:00.000Z\")"}});
an example of the data is
{
"_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")
}
Upvotes: 3
Views: 1480
Reputation: 151142
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.
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.
See Dates in the Data Types section of the documentation for more information.
Upvotes: 3