I am using C# Mongo DB Driver for database operation.
I want to get the list of records order by the way records are inserted. Because I believe MongoDB by default sorts data on it’s primary key (e.g. _Id),
I want to get the list of records by the order based on the way it was inserted. (I don’t want to store DateTime data in table document).
Thanks in advance!
Yes, you’re right. If you don’t provide a value of
_idfield on insertion, MongoDB will generate one. And those values will increase with time. So, if you sort by_idin ascending order, you’ll get insertion order (accurate to the second, if I’m not mistaken).For capped collections, there’s a special sorting order available:
$natural. It is a guaranteed insertion order (but only for capped collections).