Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is BinData a Mongo ObjectId?

Tags:

mongodb

my team is on MongoDB 2.4.10. The id fields look like this

{ "_id" : BinData(3,"fEkTlzkZw8m4JJx5nB+fkw==")

I know the 3 is the BSON subtype and the value is Base64. But is this an ObjectId or not? I'm being told it's not. What I need is an ObjectId getTimestamp() method.

like image 792
dan coleman Avatar asked Jan 08 '23 10:01

dan coleman


1 Answers

What you see is a UUID with legacy encoding.

BinData(3, ...), however, has nothing to do with the bson types as referenced in another answer and as you already pointed out. Of course the bson type is 0x05, since it's binary data. That shows up as BinData on the console.

However, BinData has, again, an 'internal type', so to speak, the subtype. That is the 3 you're seeing as the first parameter to the BinData constructor. As you can see from the spec, the subtype 3 specifies that it is a UUID.

\x03 UUID (Old) - This used to be the UUID subtype, but was deprecated in favor of \x04. Drivers and tools for languages with a native UUID type should handle \x03 appropriately.

That means this is not just some binary data, but unfortunately, most UUIDs (all?) don't contain a timestamp, so that information is lost I'm afraid.

Note that an ObjectId has a different bson type (0x07), that way it is a bit more efficient than BinData, because no additional subtype information needs to be stored. Differently put, BinData is never an ObjectId (unless it was explicitly stored as generic binary information, which is counterproductive)

like image 93
mnemosyn Avatar answered Jan 15 '23 05:01

mnemosyn