Mongodb set _id as decreasing index Mongodb set _id as decreasing index mongoose mongoose

Mongodb set _id as decreasing index


Short answer

No, you can't create a descending index on _id field. You also can't remove it to recreate it as a descending index. Mongo will use the default index when doing a descending sort on the _id field.

Long answer

As stated in the documentation MongoDB index on _id field is created automatically as an ascending unique index and you can't remove it.

You also don't need to create an additional descending index on _id field because MongoDB can use the default index for sorting.

To verify that MongoDB is using index for your sorting you can use explain command:

db.coll.find().sort({_id : -1}).explain();

In the output explain command, the relevant part is

"cursor" : "BtreeCursor _id_ reverse"

which means that MongoDB is using index for sorting your query in reverse order.


actually you can use this index, just put .sort({"_id":-1}) at the end of you query

ObjectId values do not represent a strict insertion order.

From documentation: http://docs.mongodb.org/manual/reference/object-id/

IMPORTANT The relationship between the order of ObjectId values and generation time is not strict within a single second. If multiple systems, or multiple processes or threads on a single system generate values, within a single second; ObjectId values do not represent a strict insertion order. Clock skew between clients can also result in non-strict ordering even for values, because client drivers generate ObjectId values, not the mongod process.