Chinmay Sarupria Chinmay Sarupria - 1 year ago 105
Javascript Question

Firebase: How to force database to store values in the order they are entered?

At the moment, Firebase doesn't stores the data in the order it is entered. For ex - There could be 2 existing childs and a new child is inserted, that new child goes and sits in the middle of the already existing ones! I guess this works by alphabet order but I want to store data in the correct order only.

//Could insert anywhere

I think push could insert it in the correct order but push also generates a unique key which I don't want, I want to insert data only using set and in the correct order.

Answer Source

It is not possible to insert data into Firebase into a desired position. Firebase data is sorted alphabetically and all newly inserted data will be rearranged accordingly.

Use query rules to return the data in the order you want.

Such as:

  • orderByChild()
  • orderByKey()
  • orderByValue()
  • equalTo()

You are correct about push(), it will always create a unique key and add to the end, just as pushing to an array will add to the end.