partizal
partizal

Reputation: 33

How can I add two collection into collection android studio in firestore

I'm trying to make a lesson add system using Firestore in android studio.

When the user adds a lesson, it will be added to Firestore and there will be a student and teacher collection in that lesson collection.

But I don't know how to do it.

If I write code for student and teacher collections, it adds two different collections, but I want to have 2 collections, both student and teacher, in one lesson collection.

How can I do this?

I share the code that I wrote.

add lesson class

private ActivityDersEkleBinding binding;
private FirebaseFirestore mFirestore;
private String txt_lesson;
private   LinkedHashMap<String,String> linkedHashMap;
private int i=1;
private CollectionReference Courses;

public void init(){
    linkedHashMap = new LinkedHashMap<>();
    mFirestore = FirebaseFirestore.getInstance();
    Courses = mFirestore.collection("Courses");
    Ders_EKLE();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityDersEkleBinding.inflate(getLayoutInflater());
    View view = binding.getRoot();
    setContentView(view);
    init();
}

private void Ders_EKLE(){

    //Ders ekleme tam olmadı. Tekrar bakılacak.

    binding.btnEkleDers.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            txt_lesson = binding.isimDers.getText().toString();

     //Show alertdialog if the user has entered an empty lesson 

            if(txt_ders.equals("")){
                AlertDialog.Builder builder = new AlertDialog.Builder(DersEkleActivity.this);
                builder.setTitle("WARNING!");
                builder.setMessage("you cant empty value !");
                builder.setIcon(R.drawable.warningicon);
                builder.setPositiveButton("Tamam", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                });
                builder.show();
            }
            else {

         //If a lesson has been entered, add it to the firestore.

                linkedHashMap.put("lesson"+i,txt_lesson); 


                int j=1;
                Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
                        .document()
                        .collection(txt_lesson)
                        .document()
                        .collection("Student")
                        .add(new DersEklePerson())
                        .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                            @Override
                            public void onSuccess(@NonNull DocumentReference documentReference) {
                                Toast.makeText(DersEkleActivity.this,"Ders başarıyla eklendi.",Toast.LENGTH_LONG).show();
                            }
                        }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {

                    }
                });





            }

        }
    });

}

EDIT enter image description here

Upvotes: 0

Views: 809

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598901

The Firestore data model is:

  1. At the top level you have collections.
  2. In each collection you (can) have documents.
  3. Each each document you can have nested collections again.
  4. Etc.

So a collection can either exist at the root level, or under a document. You cannot have a collection directly inside a collection, as you repeatedly say you want in your question.


It is also most common to have symbolic names that you can specify in your code for collections such as users, courses, lessons, questions, etc. Then you can generate the IDs for the documents inside those collection.

You data model seems to not follow these common patterns, which I think is part of the reason you are running into problems.


Let's have a look at this code, with the knowledge above:

Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
        .document()
        .collection(txt_lesson)
        .document()
        .collection("Student")
        .add(new DersEklePerson())
  • It starts good: Courses sounds like the name of a collection, and the document ID you have in there looks generated, so 👍

  • The Lessons subcollection also look good. 👍

  • But when you call document() like this, it generates a new document reference to a new, non-existing document, which seems unlikely to be what you want.

  • More likely you want the lesson document that is identified by txt_lesson, which you'd do with document(txt_lesson).

  • If you do that, you also don't need the second document() call anymore.

  • Then you have a Student subcollection, which looks good again. 👍

    Well, I'd recommend calling it Students, since your other collection names are also plural.

  • And then finally you call add() on that Student collection to generate a document for the student. 👍

So after all of that, I think you need:

Courses.document("KDXnJKKno1D2xtG9G6UE").collection("Lessons")
        .document(txt_lesson)
        .collection("Student")
        .add(new DersEklePerson())

Upvotes: 1

Related Questions