Discussion:
Create MongoDB ObjectID from date in the past using PHP driver
MBiff
2013-01-17 02:39:34 UTC
Permalink
I have to import a lot of data into MongoDB from MySQL and I'd like to use
the timestamp from the ObjectID instead of storing it in a separate
key/value (as it is in the existing data). In order to do this I'd need to
create an ObjectID for the existing data with a date from the past. I also
need to do this using the PHP driver. It seems like maybe there is a way to
do this in Python, Java and Node.JS so I thought maybe there was an
equivalent method in PHP.

If this is possible - is it safe to do? Meaning and I going to have issues
with duplicate or invalid ObjectIDs? Thanks.

In Python (this is maybe only for doing queries?):
gen_time = datetime.datetime(2010, 1, 1)
dummy_id = ObjectId.from_datetime(gen_time)

In Java:
Date d = new Date(some timestamp in ms);
ObjectId id = new ObjectId(d)

In node
var timestamp = Math.floor(new Date().getTime()/1000);
var objectId = new ObjectID(timestamp);
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Sam Millman
2013-01-17 10:09:08 UTC
Permalink
Also here:
http://stackoverflow.com/questions/14370143/create-mongodb-objectid-from-date-in-the-past-using-php-driver/14376613
Post by MBiff
I have to import a lot of data into MongoDB from MySQL and I'd like to use
the timestamp from the ObjectID instead of storing it in a separate
key/value (as it is in the existing data). In order to do this I'd need to
create an ObjectID for the existing data with a date from the past. I also
need to do this using the PHP driver. It seems like maybe there is a way to
do this in Python, Java and Node.JS so I thought maybe there was an
equivalent method in PHP.
If this is possible - is it safe to do? Meaning and I going to have issues
with duplicate or invalid ObjectIDs? Thanks.
gen_time = datetime.datetime(2010, 1, 1)
dummy_id = ObjectId.from_datetime(gen_time)
Date d = new Date(some timestamp in ms);
ObjectId id = new ObjectId(d)
In node
var timestamp = Math.floor(new Date().getTime()/1000);
var objectId = new ObjectID(timestamp);
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Derick Rethans
2013-01-17 13:24:42 UTC
Permalink
Post by MBiff
I have to import a lot of data into MongoDB from MySQL and I'd like to
use the timestamp from the ObjectID instead of storing it in a
separate key/value (as it is in the existing data). In order to do
this I'd need to create an ObjectID for the existing data with a date
from the past. I also need to do this using the PHP driver. It seems
like maybe there is a way to do this in Python, Java and Node.JS so I
thought maybe there was an equivalent method in PHP.
If this is possible - is it safe to do? Meaning and I going to have
issues with duplicate or invalid ObjectIDs? Thanks.
Right now, the PHP driver has no built in functionality for this, the
__set_state() that the other answer mentioned is only for being able to
session-deserialize the ID and doesn't allow you to create it through
the specific components.

You will have to do the following to automatically create an ID:

<?php
function createId( $yourTimestamp )
{
static $inc = 0;

$ts = pack( 'N', $yourTimestamp );
$m = substr( md5( gethostname()), 0, 3 );
$pid = pack( 'n', posix_getpid() );
$trail = substr( pack( 'N', $inc++ ), 1, 3);

$bin = sprintf("%s%s%s%s", $ts, $m, $pid, $trail);

$id = '';
for ($i = 0; $i < 12; $i++ )
{
$id .= sprintf("%02X", ord($bin[$i]));
}
return new MongoID($id);
}

var_dump( createId( time() ) );
?>

cheers,
Derick
--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
twitter: [ "@derickr", "@mongodb" ]
}
Sam Millman
2013-01-17 14:25:32 UTC
Permalink
+1 That was the answer I was looking to provide :)
Post by Derick Rethans
Post by MBiff
I have to import a lot of data into MongoDB from MySQL and I'd like to
use the timestamp from the ObjectID instead of storing it in a
separate key/value (as it is in the existing data). In order to do
this I'd need to create an ObjectID for the existing data with a date
from the past. I also need to do this using the PHP driver. It seems
like maybe there is a way to do this in Python, Java and Node.JS so I
thought maybe there was an equivalent method in PHP.
If this is possible - is it safe to do? Meaning and I going to have
issues with duplicate or invalid ObjectIDs? Thanks.
Right now, the PHP driver has no built in functionality for this, the
__set_state() that the other answer mentioned is only for being able to
session-deserialize the ID and doesn't allow you to create it through
the specific components.
<?php
function createId( $yourTimestamp )
{
static $inc = 0;
$ts = pack( 'N', $yourTimestamp );
$m = substr( md5( gethostname()), 0, 3 );
$pid = pack( 'n', posix_getpid() );
$trail = substr( pack( 'N', $inc++ ), 1, 3);
$bin = sprintf("%s%s%s%s", $ts, $m, $pid, $trail);
$id = '';
for ($i = 0; $i < 12; $i++ )
{
$id .= sprintf("%02X", ord($bin[$i]));
}
return new MongoID($id);
}
var_dump( createId( time() ) );
?>
cheers,
Derick
--
{
website: [ "http://mongodb.org", "http://derickrethans.nl" ],
}
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To unsubscribe from this group, send email to
See also the IRC channel -- freenode.net#mongodb
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongodb-user-/***@public.gmane.org
To unsubscribe from this group, send email to
mongodb-user+unsubscribe-/***@public.gmane.org
See also the IRC channel -- freenode.net#mongodb
Loading...