8

I want to use mongodb's default _id but in decreasing order. I want to store posts and want to get the latest posts at the start when I use find(). I am using mongoose. I tried with

postSchema.index({_id:-1})

but it didn't work

> db.posts.getIndexes()
  [
{
    "v" : 1,
    "key" : {
        "_id" : 1
    },
    "name" : "_id_",
    "ns" : "mean-dev.posts"
}]

I dropped the database and restarted mongod. No luck with that. Is there any way to set _id as a decreasing index at the sametime using mongodb's default index? I don't want to use sort() to sort the result according to _id decreasingly.

ma08
  • 3,654
  • 3
  • 23
  • 36

2 Answers2

10

Short answer

You cannot a descending index on _id field. You also don't need it. Mongo will use the existing 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.

Christian P
  • 12,032
  • 6
  • 60
  • 71
0

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.

marcinn
  • 1,786
  • 11
  • 13
  • I know that sort works but that will not be efficient to sort it every time I make a query. My question was - is it possible to set it as a decreasing index? – ma08 Jun 30 '14 at 10:43
  • no, and on top of that its not quaranteed that the order ObjectIds will be consistent. ObjectId consists of timestamp, processID, machine id. It can be set by the driver, client. So its not 100% you will get it in the order, the records were inserted. – marcinn Jun 30 '14 at 10:51
  • The first x bits of the ObjectId is timestamp if I am correct. So it can be used to sort according to time. Anything wrong with that? – ma08 Jun 30 '14 at 10:57
  • @marcinn Thanks for clarifying the issue with ObjectId timestamp. I was aware of it. I can live with some minor ambiguity in my use case, atleast for now. – ma08 Jun 30 '14 at 11:12
  • They do represent order, if they're generated manually using uuid version 1 which is also called time uuid. They're just as good as another manually created field which contains submission date. – Sebastian Nowak Mar 09 '15 at 13:01