Matt Costa
Matt Costa

Reputation: 217

How to handle custom user ordering in Firestore

From the context of a todo application, the user has a list of todos, if they reorder an item anywhere in the list how could that be saved in Firebase Firestore?

I currently have a collection with ALL todos. They get filtered by user ID and day, but I’d like to allow for custom ordering of todos. How could that be achieved?

Upvotes: 2

Views: 844

Answers (2)

KevinH
KevinH

Reputation: 324

In order to maintain the previous costs while also allowing todo items to be ordered per user you can utilize a new field per document in the todo collection. You can attempt to add a field, named lets say "order", which will hold the numerical value for the order of the todo element for each user. For example "0" for first position,"1" for second, and so on so forth. These will then be filtered by user ID and day as mentioned previously.

Upvotes: 1

Akshay Jain
Akshay Jain

Reputation: 904

There is no inbuilt solution available for this. The solution I will suggest is to keep a separate document per user which maintains the order of documents (in this case to do items) in an array and when you show the data to the user in the UI, use the document to order the items on the page. But keep in mind this approach will increase your database costs because you will need to perform an update to 2 documents whenever a new todo is created.

Upvotes: 1

Related Questions