Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Decode and Encode object ids in MongoDB using pymongo
# MongoDB Object ID decode
# Randall Shane, PhD
# 28 July 2014
'''MongoDB creates a unique id ('_id' key) for every document
which is a 12 byte object comprised of 4 parts as follows:
time stamp: 4 bytes
machine id: 3 bytes (MD5 hash of host name/mac)
process id: 2 bytes (first 2 bytes of the PID)
unique id: 3 byte integer value
Decode and encode as follows'''
from bson import objectid
from datetime import datetime
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
db = client['test']
coll = db['testData']
def decode(oid):
"""Output is a dict containing:
{ oid: (str) Original Object ID,
ets: (int) Epoch TimeStamp,
fts: (str) Formatted TimeStamp,
mid: (int) Machine ID,
pid: (int) Process ID,
uid: (int) Unique ID }"""
out = {}
out['oid'] = str(objectid.ObjectId(oid))
out['ets'] = int(out['oid'][0:8], 16)
out['mid'] = int(out['oid'][8:14], 16)
out['pid'] = int(out['oid'][14:18], 16)
out['uid'] = int(out['oid'][18:], 16)
out['fts'] = datetime.fromtimestamp(out['ets'])\
.strftime('%d %b %Y %H:%M:%S')
print out # for display
return out
def encode(data):
"""Input options is a dict of the 4 components
{ ets: (int) Epoch TimeStamp,
mid: (int) Machine ID,
pid: (int) Process ID,
uid: (int) Unique ID }"""
oid = hex(data['ets']).replace('0x', '')
oid += hex(data['mid']).replace('0x', '')
oid += hex(data['pid']).replace('0x', '').zfill(4)
oid += hex(data['uid']).replace('0x', '').zfill(6)
print oid
return oid
if __name__ == '__main__':
# Decode on a collection created in the MongoDB shell by:
# for (var i = 0; i <= 5; i++) db.testData.insert( { x : i } )
print 'DECODE:'
[decode(x['_id']) for x in coll.find({}, {'_id': 1})]
# Encode test creating ObjectId using specific information
print 'ENCODE:'
data = {'ets': 1406557108, 'mid': 9999, 'pid': 8888, 'uid': 7777}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment