mongodb - Database and item orders (general) -


i'm right experimenting nodejs based experimental app, putting in list of books , posted on forum automatically every x minutes.

now question order of these things posted.

i use mongodb (not sure if changes question or not) , add new entry every item posted. normally, things posted in exact order add them.

however, web interface of experimental thing, made re-ordering interaction can drag , drop elements reorder them.

my question is: how can reflect change database?

or more in general terms, how can order stuff in general, in databases?

for instance if drag 1000th item 1st order, below needs edited (in db) between 1 , 1000 entries. not seem valid , proper solution me.

any enlightenment appreciated.

an elegant way might lexicographic sorting. introduce string attribute each item. make initial length of values large enough accomodate estimated number of items. e.g., if expect 1000 items, let keys baa, bab, bac, ... bba, bbb, bbc, ...

then, when item moved place between 2 items, assign value sorting attribute of moved item somewhere equidistant (lexicographically) items. move item between dei , dej, give value deim. move item between fadd , fado, give value fadi.

keys starting not used leave space elements dragged before first one. never use key a, impossible move element before one.

of course, characters used may vary according sort order provided database.

this solution should work fine long elements not reordered extremely frequently. in worst case scenario, may lead longer , longer attribute values. if movements equally distributed, length of values should stay reasonable.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -