Insertar objetos entre otros objetos en una colección ordenada

I have a sorted (Mongodb) collection of objects:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Marge", "order": 2}

I would like to prepend, append or insert between two objects another object. I've tried this using float numbers like this:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Bart", "order": 1.5},
{"_id": ObjectId(...), "person": "Marge", "order": 2}

Order value is the arithmetic mean of fields order in the previous and the next object. It works works as long as numbers don't look like 1.9999999. Then it is rounded to 2 and the collection isn't sorted. I could use integers in this way:

{"_id": ObjectId(...), "person": "Homer", "order": 1},
{"_id": ObjectId(...), "person": "Bart", "order": 2},
{"_id": ObjectId(...), "person": "Marge", "order": 3}

but it means the need to modify the other objects and I would rather avoid that.

I thought about using Strings and lexical sorting to prevent order:

{"_id": ObjectId(...), "person": "Homer", "order": aa},
{"_id": ObjectId(...), "person": "Bart", "order": ab},
{"_id": ObjectId(...), "person": "Marge", "order": bb}

but it seems difficult to implement. Is there any easier way to do it?

preguntado el 27 de julio de 12 a las 16:07

What is the "order" for? You look like you have the objects in order anyway so I'm not sure why you need to set the order item. Can you not just set it on output or something? -

I query and then display them. User can modify their order, after that objects are saving into Mongodb. Next time I would like to present them in same order. -

I can't imagine there is that much overhead in just renumbering. Otherwise your alphabetical sorting should work fine I'd have though. -

That would work if there would be only a few objects after the inserted object. I fear a decrease of performance when there are hundreds of them. I would like to just update or insert a few objects and don't touch others. -

1 Respuestas

I think that the floats approach makes the most sense. Assuming that you start off with all locations as integers, it would take a montón of swaps until you run into precision problems.

I suggest that whenever you change the "order" of an item, see if it's too close to one of its neighbors. If it is, reassign the values of todos of the objects (so they are all integers again).

If you set a limit of |x-y| >= 2**(-50), which is fine since python keeps 53 precision bits for floats, it will take at least 50 unlucky swaps before you need to reassign the orders.

Editar: I just saw that you didn't ask about Python specifically. Replace the precision with whatever makes sense for your system.

Respondido 27 Jul 12, 16:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.