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.