I wanted to implement a function which needs the values from a pymongo collection in the reverse order of inserting.
I can think of a couple of ways:
cursor = collection.find(skip=collection.count()-LIMIT_VAL,limit=LIMIT_VAL)
rows = list(cursor).reverse()
This way would be a little bad since LIMIT_VAL is a variable and I do not want to put the entire thing into memory. I do not want to supply -ve value to the skip parameter.
OR
rows = collection.find(sort={'$natural':-1}, limit=LIMIT_VAL)
I read here that you can not really rely on natural sort order. I do not know how that can be unreliable
OR
I insert a value called order_of_insert into each record and do a sort on that ensuring that I get how I have inserted.
Before I implement this I needed to make sure which of the ways would be the best thing to do.
Mongodb ObjectIDs store time-stamp information accurate up to the second. $natural sorts it by this _id. If your app were to insert a document within the same second, the insertion order would be ambiguous. $natural would correspond to insertion order only if your collection were capped.