CouchDB - Sorting Documents by Position

What is the best practice for sorting / ordering multiple documents according to user order (position) in CouchDB?

The solutions I was thinking about:

  • Each document has a position value, starting from 1 to n. The view will emit this value. Problem. If one document is sorted, all other documents with a higher position should be updated. It could be hundreds of updates. Hm.

  • Each document knows its previous _id document. The order is created after receiving the submission.

  • This is a special document storing _ids of all documents that must be sorted in an array. We sort this solution again after receiving the submission.

Is there any other or simpler solution? The RDBMS 1. solution had the best practice, and a simple update request completed an update of the position of all documents.

Regards, Bernd

+4
source share
1 answer

You can use a float value, for example, between 0.0 and 1.0 for a position. To move document A between documents B and C, you simply set its new position to (B.position + C.position) / 2.0 (i.e. the average of their positions).

+4
source

Source: https://habr.com/ru/post/1303706/


All Articles