I have a collection "collection_Save" in mongoDB that contains documents that are used to save the operations that occur on others documents in an other collection. They are listed by order of creation in the database. In order to reverse those operations I need to run through the collection from the end to the start.
This is where I can't figure out how to do. Since MongoCollection doesn't have the equivalent of a "Reverse" method.
I tried to create an index using the following code
collection_Save.CreateIndex(IndexKeys<SaveMongo>.Ascending(_ => _._id));
but I can't figure out how to use it (or if it is really helpful in my case).
I did find something that might be useful : MongoRestore, skip n first documents
However they are not working in c# and my low reputation prevents me from commenting the post.
Do you know how to run through a collection in "reverse mode" ?
Just create an index on your date
field like this:
db.collection_Save.createIndex({date: -1})
then you can query your collection in this way:
db.collection_Save.find().sort({date: -1}).skip(last_n).limit(1)
where last_n
is the number (counted from the end) of the document you want to get.