Lilach26
Lilach26

Reputation: 23

How to get document ID for adding collection to it - android studio firestore

I'm developing Recipes app, so that user can register to the app and save his own recipes. I already created class Recipe, and now I want to add recipe's collection to a specific user, using his UID, but I don't know how to retrieve the path ID for inserting the collection.

This is the image of the hierarchy of the firestore

Please, any help... the code I wrote:

public class AddRecipe extends AppCompatActivity {

    private FirebaseFirestore db;
    private FirebaseAuth mAuth;
    private CollectionReference usersRef;
    private FirebaseUser user;
    DocumentReference docRef;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        db = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance();
        usersRef = db.collection("Users");


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_recipe);
    }


    public void addRecipeFunc(View view) {

        user = mAuth.getCurrentUser();
        String uid = user.getUid();
        HashMap<String, String> ingredients = new HashMap<>();
        ingredients.put("Banana", "3");
        Recipe recipe = new Recipe("bla", "bla", ingredients, "bla", "bla", "bla");

        usersRef = db.collection("Users");
        //usersRef.document(uid).collection("Recipes").add(recipe);
        db.collection("Users").document(uid).collection("Recipes").document("Recipe").set(recipe);
    }
}

Upvotes: 2

Views: 1121

Answers (1)

Toygur Ziya Kutlu
Toygur Ziya Kutlu

Reputation: 287

db.collection("Users").document(uid).collection("Recipes").document("Recipe").set(recipe);

That code will add Recipe to Recipe collection with a document called Recipe. That means it will overwrite Recipe document everytime user want to add new Recipe. If you don't give a name to document, Firestore will create document and will give an id itself.

db.collection("Users").document(uid).collection("Recipes").document().set(recipe);

You can get DocumentId with getId() method calling with DocumentReference.

DocumentReference ref =  db.collection("Users").document(uid).collection("Recipes").document();
//Id of the document
String docId = ref.getId();
ref.set(recipe);

Or you can add another field called Id to your Recipe class (don't forget to add that field in to constructor) and you can set your own Id. This will also save your Id in to document.

String id = String.valueOf(System.currentTimeMillis());
Recipe recipe = new Recipe(id, "bla", "bla", ingredients, "bla", "bla", "bla");
DocumentReference ref =  db.collection("Users").document(uid).collection("Recipes").document(id).set(recipe);

Edit: If you want to add all Recipe objects inside user document without creating subcollection, create Map and store all recipes inside that.

Map<String, Recipe> recipes = new HashMap<>;
recipes.put("recipe1", new Recipe("bla", "bla", ingredients, "bla", "bla", "bla"));
recipes.put("recipe2", new Recipe("bla2", "bla2", ingredients2, "bla2", "bla2", "bla2"));

And update that document with your Map. field is the field name for Map (user's document needs a field for Map).

DocumentReference ref =  db.collection("Users").document(uid);
db.runTransaction((Transaction.Function<String>) transaction -> {
    transaction.update(ref, field, recipes);
    return null;
});

Upvotes: 1

Related Questions