Home > OS >  update a Array without duplicate Map based on a parameter
update a Array without duplicate Map based on a parameter

Time:01-27

I am currently using ArrayUnion to update an Array in a Document in Firebase. It Consists of a Map of the format {userid:XXXXXX,value:01234}. Now lets say I want to change this value, I was initially thinking of just using ArrayUnion and send an updated map, But how do I remove the pre-existing Map. (In Other Words No Too Map in the array can have the same ID).

Seems like a long shot, but I was also thinking maybe we can just use a Map which has a timestamp attribute to showing when it was added, and then on the client side ill perform the actions of removing those entries which are older.

Which of the options is better? and do you have any better solutions for the same problem?
(Note: Using Firebase v9 for Web)

CodePudding user response:

The only way you can remove a map item from an array without reading the document first is if you know the entire contents of the map ahead of time. If you do, you can use arrayRemove, passing the entire map as an argument.

If you don't know the entire map contents, then your only option is to read the document into memory, modify the array the way you want in memory, then write the entire updated array field back to the document.

It is not possible to do anything with a map inside an array if you don't know its entire contents. If you only know one field, you can't update it and you can't use it in a query.

See:

  • Related