Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8998947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:06:35+00:00 2026-06-16T00:06:35+00:00

From the MongoDB console, I can: > db.log.insert({ dt : new Date }) >

  • 0

From the MongoDB console, I can:

>  db.log.insert({ dt : new Date })
>  db.log.find().sort({ $natural : -1 }).limit(1)
{ "_id" : ObjectId("50caae2cadd0e471af0b3941"), "dt" : ISODate("2012-12-14T04:42:20.560Z") } 

How can I do the same from the Perl MongoDB driver?

Background

I am using MongoDB with a capped collection for logging. I understand that the ObjectID contains a timestamp, but displaying it and querying it is not simple. I would therefore like to add a server-based timestamp to each entry, but have been unable to figure out how to pass literal commands via the Perl driver.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:06:36+00:00Added an answer on June 16, 2026 at 12:06 am

    The Perl MongoDB Driver uses DateTime objects to store Dates, and returns DateTime object back to you if you read dates from the database.

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use MongoDB;
    use MongoDB::OID;
    use DateTime;
    
    # database connection & collection
    my $conn  = MongoDB::Connection->new;
    my $db    = $conn->dates;
    my $log   = $db->log;
    
    # Delete Collection
    $log->drop;
    
    # Insert one
    my $oid = $log->insert({ dt => DateTime->now });
    
    # Show all
    my $all = $log->find;
    while ( my $entry = $all->next ) {
        printf "_id: %s dt: %s\n", $entry->{_id}->to_string, $entry->{dt}->format_cldr("dd.MM.yyyy HH:mm:ss");
    }
    

    If you execute it, it prints something like

    _id: 50cb1b9321d30efd17000000 dt: 14.12.2012 12:29:07
    

    You should also read the DateTime Documentation to understand what you can do with it: http://search.cpan.org/perldoc?DateTime

    On the Connection startup you can also set which Date object you want.

    http://search.cpan.org/perldoc?MongoDB::Connection#dt_type

    If you set it to DateTime::Tiny for example it will be faster. But you should first understand the difference between DateTime and DateTime::Tiny. Read the Documentation of booth and decide in which case the one or the other is better.


    The example above creates the client time. If you want the time from the server instead of your client, because client/server are not the same. You can do two things.

    At first, create a JavaScript function that returns a Date object und evaluate the function on the server.

    # JavaScript function that return a Date object
    my $now = MongoDB::Code->new(code => qq{function(){
        return new Date
    }});
    
    # later...
    my $oid = $log->insert({ 
        dt => $dt,
        st => $db->eval($now),
    });
    

    The second possibility. The default “_id” object from MongoDB already contains a timestamp when the object will be created. At least the documentation says it comes from the server, and i hope it really comes from the server and will not created by the driver. But if $entry is your MongoDB result you can get a DateTime object from it this way:

    my $dt = DateTime->from_epoch(epoch => $entry->{_id}->get_time);
    

    Example:

    #!/usr/bin/env perl
    use strict;
    use warnings;
    use MongoDB;
    use MongoDB::OID;
    use MongoDB::Code;
    use DateTime;
    
    # database connection & collection
    my $conn  = MongoDB::Connection->new;
    my $db    = $conn->dates;
    my $log = $db->log;
    
    # Delete Collection
    $log->drop;
    
    # Client Time
    my $dt = DateTime->now;
    
    # JavaScript function that return a Date object
    my $now = MongoDB::Code->new(code => qq{function(){
        return new Date
    }});
    
    # wait 2 seconds to see a difference between
    # $dt and the $now function
    sleep 2;
    
    # Insert one
    $log->insert({ 
        dt => $dt,
        st => $db->eval($now),
    });
    
    sleep 2;
    
    $log->insert({ 
        dt => $dt,
        st => $db->eval($now),
    });
    
    # Show all
    my $dtf = "dd.MM.yyyy HH:mm:ss";
    my $all = $log->find;
    while ( my $entry = $all->next ) {
        printf "dt: %s\n", $entry->{dt}->format_cldr($dtf);
        printf "st: %s\n", $entry->{st}->format_cldr($dtf);
        printf "_id time: %s\n", DateTime->from_epoch(epoch => $entry->{_id}->get_time)->format_cldr($dtf);
        print "\n";
    }
    

    Output:

    dt: 14.12.2012 16:29:28
    st: 14.12.2012 16:29:30
    _id time: 14.12.2012 16:29:30
    
    dt: 14.12.2012 16:29:28
    st: 14.12.2012 16:29:32
    _id time: 14.12.2012 16:29:32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can you drop a numeric collection from MongoDB? PRIMARY> db.123456789011.remove({}); Tue Mar 20
I have a web page where users can see data from MongoDB on a
I'm trying to insert a large amount of text into a mongodb collection from
I'm attempting to use the new mongodb aggregation framework from php. I'm using mongod
Here's the relevant code: app.get('/all', function(req,res) { Party.find({},[],function(p) { console.log(p); }); res.redirect('/'); }); should
Trying to insert into a mongodb database from scala. the below codes dont create
does anyone know how I can use a database-name like abc-123 from the mongodb
I wanted to extract data from mongodb and pass it to the view. Everything
I am using mongoose/nodejs to get data as json from mongodb. For using mongoose
I know that it is possible read and write data from mongodb via hadoop.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.