Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Data::Dumper;
- use DateTime::Format::MySQL;
- use MongoDB;
- # parse the date
- my $dt = DateTime::Format::MySQL->parse_datetime( '2003-01-16 23:12:01' );
- # connect to the DB and get a MongoDB::Collection to work with
- my $mc = MongoDB->connect("mongodb://localhost:27017");
- my $coll = $mc->ns('test.foo');
- # insert
- $coll->insert_one({ date => $dt });
- # query
- my $doc = $coll->find_one({date => $dt});
- # dump the doc -- note the date is now a BSON::Time and there's a
- # MongoDB '_id' field added
- say Dumper $doc;
- # $VAR1 = {
- # 'date' => bless( {
- # 'value' => '1042758721000'
- # }, 'BSON::Time' ),
- # '_id' => bless( {
- # 'oid' => '[rðôZN@Yä
- # '
- # }, 'BSON::OID' )
- # };
- # get it back as a DateTime object and print it out
- my $date = $doc->{date}->as_datetime;
- say $date;
- # 2003-01-16T23:12:01
Add Comment
Please, Sign In to add comment