问题描述:

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.

网友答案:

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)

网友答案:

This is not a ObjectId, 3 is for type Object

You can simply verify this on Mongo client:

> var a = { "_id" : BinData(3,"fEkTlzkZw8m4JJx5nB+fkw==")}

> a._id instanceof Object

true is returned while on trying

> a._id instanceof ObjectId

false is returned. For more details click.

相关阅读:
Top