0

I got success in uploading a nexted map into firestore database like

 Map<String, Map<String,Map<String,Map<String,Boolean>>>> Fleet = new HashMap<>();

But I am not able to retrieve this map Task method. I do not understand how to create an object for this kind of map.

  • MainItem - SubItem -- Item: Key - True/False This is the structure in firebase firestore database.

Kindly help me to solve this problem.

DynamicMind
  • 4,240
  • 1
  • 26
  • 43
  • refer:- https://stackoverflow.com/questions/3636834/mapstring-mapstring-boolean-mymap-new-hashmapstring-hashmapstring-bool – Ali Feb 15 '18 at 12:02

1 Answers1

0

To achieve this, i can give you an example. Let's take a POJO class named users. This class should look like this:

public class UserModel {
    private String userEmail, userName;
    private Boolean admin;

    public UserModel() {}

    public UserModel(String userEmail, String userName, Boolean admin) {
        this.userEmail = userEmail;
        this.userName = userName;
        this.admin= admin;
    }

    public String getUserEmail() {return userEmail;}
    public String getUserName() {return userName;}
    public Boolean getAdmin() {return admin;}
}

To add a user to the database, please use the following code:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
UserModel userModel = new UserModel("DynamicMind@email.com", "DynamicMind", true);
rootRef.collection("users").document(userEmail).set(userModel)

I have also userd a Boolean property, to see more clearly.

Unlike in Firebase Realtime database, where we are using objects nested beneath other objects, in Cloud Firestore we use Collections and Documents. So, your database will look like this:

Firestore-root
    |
    --- users (collection)
          |
          --- DynamicMind@email.com (document)
                  |
                  --- userEmail: "DynamicMind@email.com" (property)
                  |
                  --- userName: "DynamicMind" (property)
                  |
                  --- admin: true

If you want to learn more about structuring a Cloud Firestore database using model classes, I recommend you see one of my tutorials, in which I have explained step by step how to achieve this.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193