firebase realtime database set data code example

Example 1: add to firebase database

db.collection("cities").add({
    name: "Tokyo",
    country: "Japan"
})
.then(function(docRef) {
    console.log("Document written with ID: ", docRef.id);
})
.catch(function(error) {
    console.error("Error adding document: ", error);
});

Example 2: javascript write to firebase

function writeUserData(userId, name, email, imageUrl) {
  firebase.database().ref('users/' + userId).set({
    username: name,
    email: email,
    profile_picture : imageUrl
  });
}

Example 3: firebase realtime database read data

If you want to get a specific data using a phone number then you can use a Query like this

DatabaseReference database = FirebaseDatabase.getInstance().getReference();
DatabaseReference ref = database.child("profiles");

Query phoneQuery = ref.orderByChild(phoneNo).equalTo("+923336091371");
phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
           user = singleSnapshot.getValue(User.class); 
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e(TAG, "onCancelled", databaseError.toException());
    }
});
I think this is the best method.