portfoliobuilder
portfoliobuilder

Reputation: 7856

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?

Upvotes: 4

Views: 9118

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

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.

Upvotes: 11

Related Questions