Posts Tagged ‘bson’

MongoDB ObjectID for AS3

Posted in to do on April 2nd, 2012 by ericsoco – 4 Comments

I’ve been working with MongoDB as of late, and finding the offerings for an ActionScript 3.0 driver to be rather lacking.

I started with ActionMongo, and quickly found out that it has no support for much of anything except the find() command.  I then found my way to MongoAS3, which is a HUGE improvement over the barely-started functionality within ActionMongo.  (Credit where credit’s due, Omar Gonzales built MongoAS3 off of Claudio Alberto Andreoni’s first steps with ActionMongo.)

However, I found a missing piece in MongoAS3 that was holding me back: it doesn’t provide any way to generate a new ObjectID.  This is a problem if, for example, you want to insert a new document.  Since Mongo doesn’t provide a response on a successful insert, this means that a followup query is required to get the ObjectID assigned by the server to the newly-inserted document.  Not ideal.

So, I added functionality to ObjectId.as (which Omar included from Claudio’s original, as part of ActionBSON) to generate a new ObjectID per the ObjectID spec.  The one deviation from the spec comes from the difficulty in getting the process ID from within a flash runtime (requires launching a NativeProcess, which can only be done from an AIR application with the extendedDesktop profile); for this, we’re just using a random.

I also added the ability to extract the timestamp from the ObjectID, which could prove useful.

Hope it’s useful to some of you…I’ve submitted it to Claudio and Omar for inclusion into their distros.  Code after the break, or on Github. (I need a better Gist plugin…)

read more »