Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple values under the same key for FireBase?

How do I add multiple values for the same key in Firebase? When I do the following to add data to my database, the objects are just replacing each other.

    Map<String,Object> taskMap = new HashMap<>();
    taskMap.put("age", "12");
    taskMap.put("gender", "male");
    taskMap.put("age", "45");
    taskMap.put("gender", "female");
    reference.setValue(taskMap);

The data in Firebase only displayed age 45, and gender female. It overrode the age 12 and gender male. How do I have both?

like image 787
portfoliobuilder Avatar asked Oct 23 '25 13:10

portfoliobuilder


1 Answers

To update a subset of the existing properties or to add new ones, use updateChildren():

Map<String,Object> taskMap = new HashMap<>();
taskMap.put("age", "12");
taskMap.put("gender", "male");
taskMap.put("age", "45");
taskMap.put("gender", "female");
reference.updateChildren(taskMap);

Also see the documentation for updating specific fields.

like image 77
Frank van Puffelen Avatar answered Oct 25 '25 02:10

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!