rawsh
rawsh

Reputation: 425

Change order in Mongodb

I have a small todo list application. How can I preserve order when the user drags and drops?

Would keeping a json in the document with the order of the ids be the best solution? This would make me change the document whenever the user deletes a task.

Upvotes: 2

Views: 4050

Answers (1)

Kevin Smith
Kevin Smith

Reputation: 14436

MongoDB does preserve the index as you've saved it but re-ordering might be a little difficult (https://groups.google.com/forum/#!topic/mongodb-user/HfFlSInmZME) so you could try the following:

Create a new field on the object that represents the item and order on that

{ items: [ { name: "todo1", index: 0 }, { name: "todo2", index: 1 } ] }

After querying for the items just order them by the index field.

Upvotes: 1

Related Questions