The ObjectId value contains a timestamp. Actually, it begins with a timestamp ( https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectId ). All ObjectId values have a getTimeStamp() method that returns the creation time ( https://docs.mongodb.com/manual/reference/method/ObjectId.getTimestamp/#objectid-gettimestamp ). By default, document identifiers are ObjectId values in MongoDb. A unique index is created for the _id field in every collection. It would be advantageous if MongoDb could use this index (partially) to effectively filter and sort documents by their creation date stored in the ObjectId values.
However, I clould not find information about this anywhere. I know for a fact that MongoDb can use compound (multi-field) indexes partially to speed up filter/sort operations (MongoDb use indexes partially) But this question is somewhat different. The primary key's unique index is not a compound (multi-field) index. But it shares some properties with multi-field indexes. In particular, the order of ObjectId values and the order of creation dates are the same. So in theory, it would be possible to use it as a partial index. I just don't know if MongoDb can do that or not. Of course, it also depends on the index type. If the primary key's index is implemented with a hash, then it probably cannot be used for sorting and filtering, only for lookups.
Can anybody tell, if I need to create a separate "created" datetime field and an index on it, to be able to effectively sort and order documents by the creation date? (I'm using mongodb 4.2)