Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Creating a MongoDB::OID object from the _id field returned by a Perl find_one query
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Steven Hirschorn  
View profile  
 More options Oct 11 2012, 9:51 am
From: Steven Hirschorn <steven.hirsch...@gmail.com>
Date: Thu, 11 Oct 2012 06:51:24 -0700 (PDT)
Local: Thurs, Oct 11 2012 9:51 am
Subject: Creating a MongoDB::OID object from the _id field returned by a Perl find_one query

Hi,

I want to extract the timestamp from a MongoDB document's _id field in
Perl.  I've been trying to create a MongoDB::OID object from the _id string
returned by a find_one, but it complains about illegal hexadecimal digits.  
What am I doing wrong?

my $object = $inventoryColl->find_one();
my $objID = MongoDB::OID->new(value => $object->{_id});
my $time = $objID->get_time;   # this line produces an error
my $date = DateTime->from_epoch(epoch => $objID->get_time);  # also
produces error

Thanks in advance!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephen Steneker  
View profile  
 More options Oct 13 2012, 2:29 am
From: Stephen Steneker <stephen.stene...@10gen.com>
Date: Fri, 12 Oct 2012 23:29:35 -0700 (PDT)
Local: Sat, Oct 13 2012 2:29 am
Subject: Re: Creating a MongoDB::OID object from the _id field returned by a Perl find_one query

> I want to extract the timestamp from a MongoDB document's _id field in
> Perl.  I've been trying to create a MongoDB::OID object from the _id string
> returned by a find_one, but it complains about illegal hexadecimal digits.  
> What am I doing wrong?

> my $object = $inventoryColl->find_one();
> my $objID = MongoDB::OID->new(value => $object->{_id});
> my $time = $objID->get_time;   # this line produces an error
> my $date = DateTime->from_epoch(epoch => $objID->get_time);  # also
> produces error

Hi,

Assuming your _id is an ObjectID and not a custom string value, it should
already returned as a MongoDB::OID.

So this should work:

my $object = $inventoryColl->find_one();
my $objID = $object->{'_id'};
my $date = DateTime->from_epoch(epoch => $objID->get_time);
print "date: $date\n";

If you're not sure what is being returned, Data::Dumper will show you the
contents of a variable without stringifying:
  http://search.cpan.org/dist/Data-Dumper/

Cheers,
Stephen


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven Hirschorn  
View profile  
 More options Oct 15 2012, 12:26 pm
From: Steven Hirschorn <steven.hirsch...@gmail.com>
Date: Mon, 15 Oct 2012 09:26:55 -0700 (PDT)
Local: Mon, Oct 15 2012 12:26 pm
Subject: Re: Creating a MongoDB::OID object from the _id field returned by a Perl find_one query

Thanks Stephen,

Turns out the particular collection I was looking at had a custom _id
field.  When I tried against a db with a Mongo-created _id field everything
worked.  Thanks for your response.#


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »