So I have this mongodb collection called stories and each of them has an index field which corresponds to their particular ordering amongst other stories. I want to be able to reorder order these stories on a GUI and subsequently update this ordering on my database but I can't seem to figure out the best way to do this. I know that something like this would be simple if the stories were for example stored in a LinkedList data structure but I don't see how I can do this with MongoDB collections.
As an example of what I am getting at, say for example I have this collection of stories
[
{
title: 'Story 1',
index: 0
},
{
title: 'Story 2',
index: 1
},
{
title: 'Story 3',
index: 2
},
]
And I wanted to reorder the list such that Story 1 comes after Story 3, then my result in the database would be
[
{
title: 'Story 2',
index: 0
},
{
title: 'Story 3',
index: 1
},
{
title: 'Story 1',
index: 2
},
]
and then if I wanted to delete Story 3 I would be left with
[
{
title: 'Story 2',
index: 0
},
{
title: 'Story 1',
index: 1
},
]
For reference, I am trying to implement this using Javascript and Mongoose