How to move an element in a sorted list and keep the CouchDb write "atomic"

Posted by karlthorwald on Stack Overflow See other posts from Stack Overflow or by karlthorwald
Published on 2010-05-30T21:10:36Z Indexed on 2010/05/30 21:12 UTC
Read the original article Hit count: 328

I have elements of a list in couchdb documents. Let's say these are 3 elements in 3 documents:

{ "id" : "783587346", "type" : "aList", "content" : "joey", "sort" : 100.0 }
{ "id" : "358734ff6", "type" : "aList", "content" : "jill", "sort" : 110.0 }
{ "id" : "abf587346", "type" : "aList", "content" : "jack", "sort" : 120.0 }

A view retrieves all "aList" documents and displays them sorted by "sort".

Now I want to move the elements, when I want to move "jack" to the middle, I could do this atomic in one write and change it's sort key to 105.0. The view now returns the documents in the new sort order.

After a lot of sorting I could end up with sort keys like 50.99999 and 50.99998 after some years and in extreme situations run out of digits?

What can you recommend, is there a better way to do this? I'd rather keep the elements in seperate documents. Different users might edit different elements in parallel (which also can get tricky).

Maybe there is a much better way?

© Stack Overflow or respective owner

Related posts about database-design

Related posts about data-structures