| MongoDB documentation | Contained in the MongoDB distribution. |
MongoDB::OID - A Mongo ObjectId
If no _id field is provided when a document is inserted into the database, an
_id field will be added with a new MongoDB::OID as its value.
my $id = $collection->insert({'name' => 'Alice', age => 20});
$id will be a MongoDB::OID that can be used to retreive or update the
saved document:
$collection->update({_id => $id}, {'age' => {'$inc' => 1}});
# now Alice is 21
To create a copy of an existing OID, you must set the value attribute in the constructor. For example:
my $id1 = MongoDB::OID->new;
my $id2 = MongoDB::OID->new(value => $id1->value);
Now $id1 and $id2 will have the same value.
Warning: at the moment, OID generation is not thread safe.
Core documentation on object ids: http://dochub.mongodb.org/core/objectids.
The OID value. A random value will be generated if none exists already. It is a 24-character hexidecimal string (12 bytes).
Its string representation is the 24-character string.
my $hex = $oid->to_string;
Gets the value of this OID as a 24-digit hexidecimal string.
my $date = DateTime->from_epoch(epoch => $id->get_time);
Each OID contains a 4 bytes timestamp from when it was created. This method extracts the timestamp.
my $json = JSON->new;
$json->allow_blessed;
$json->convert_blessed;
$json->encode(MongoDB::OID->new);
Returns a JSON string for this OID. This is compatible with the strict JSON
representation used by MongoDB, that is, an OID with the value
"012345678901234567890123" will be represented as
{"$oid" : "012345678901234567890123"}.
Kristina Chodorow <kristina@mongodb.org>
| MongoDB documentation | Contained in the MongoDB distribution. |
# # Copyright 2009 10gen, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # package MongoDB::OID; our $VERSION = '0.43'; # ABSTRACT: A Mongo Object ID use Any::Moose;
has value => ( is => 'ro', isa => 'Str', required => 1, builder => 'build_value', ); sub BUILDARGS { my $class = shift; return $class->SUPER::BUILDARGS(flibble => @_) if @_ % 2; return $class->SUPER::BUILDARGS(@_); } sub build_value { my $self = shift; _build_value($self, @_ ? @_ : ()); }
sub to_string { my ($self) = @_; $self->value; }
sub get_time { my ($self) = @_; my $ts = 0; for (my $i = 0; $i<4; $i++) { $ts = ($ts * 256) + hex(substr($self->value, $i*2, 2)); } return $ts; }
sub TO_JSON { my ($self) = @_; return {'$oid' => $self->value}; } use overload '""' => \&to_string, 'fallback' => 1; no Any::Moose; __PACKAGE__->meta->make_immutable; 1;