4
votes

what is the best practice to sort/order multiple documents by user defined order (position) in CouchDB?

Solutions I thought about:

  1. Every document has a "position" value, starting from 1 to n. The view would emit this value. Problem: If one document is sorted, all other documents with a greater position have to updates. This could be hundreds of updates. Hmm.

  2. Every document knows it's previous document's _id. The order is generated after retrieving the view.

  3. The is a special document storing the _ids of all documents that should be sorted in an array. We sort in this solution again after retrieving the view.

Is the any other or more simple solution? In a RDBMS solution 1. was the best practice and a simple update query did the position update of all documents.

Best regards, Bernd

1

1 Answers

4
votes

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