Why is a document from a Cloud Firestore database not deleted? - java

I have the following database structure:
Firestore-root
|
--- users (collection)
| |
| --- userId (document)
| |
| --- //user details
|
--- products (collection)
|
--- listId (document)
|
--- listProducts (collection)
|
--- productId
|
--- //product details
And I use the following code to delete the documents:
rootRef.collection("users").document(userId).delete(); //works fine
rootRef.collection("products").document(listId).delete();
The first line of code works perfectly but the second is not deleting the list document. Is it because it has a subcollection beneath it or am I doing something wrong?
How to delete the entire listId document together with everything it has beneath it?
Thanks!

Collections that are nested under a document, are not automatically deleted when you delete that document. When you only delete the document itself, the Firebase console will show the document ID in italic. The document itself isn't there anymore (you can see this when you select it, as it won't have any fields), but the nested collections are still accessible.
So I suspect that the products document is gone, but the nested collections still exist. As the Firestore documentation on deleting collection describes, you'll need to delete the documents from the collection to get rid of it.

Related

How to create category containing products in firestore firebase

I am using Cloud Firestore to make my own E-commerce App. I want my category collection to contain many categories like shoes, clothes, watches, etc and in each of those categories there are different products so I can click on that category and show all the related products on my app, please guide me or if you have another way to do this, please show me.
Category and Products
According to your comment:
just tens, this is just my personal project.
Since you only have tens of category names, then the best option that you have is to store them in a single document, in an array type field. Then each product document should contain a field where you have to specify the category. Your schema should look like this:
Firestore-root
|
--- shop (collection)
| |
| --- category (document)
| |
| --- names: ["shoes", "clothes", "watches"]
|
--- products (collection)
|
--- $productId (document)
|
--- name: "Nike Air Jordan"
|
--- category: "shoes"
To be able to display the category names, you should simply read the array that exists in the document, and display the content into a ListView, or even better, in a RecyclerView.
Furthermore, if you need to click on a particular category, and go forward to display only the products that correspond to a specific category, then you should use the following query:
FirebaseFirestore db = FirebaseFirestore.getInstance();
Query queryByCategory = db.collection("products").whereEqualTo("category", "shoes");
For displaying the products you might consider reading my answer from the following post:
How to display data from Firestore in a RecyclerView with Android?
The following article might also help you read the data with the use of the Firebase-UI library:
How to read data from Cloud Firestore using get()?

FirebaseRecyclerView - make onBindViewHolder method create severals elements

I am creating a FirebaseRecyclerView.
I give to my FirebaseRecyclerView a Query that refers to objects from my FirestoreDatabase.
But instead of having 1 element for each object, I want to create severals elements.
Example of 1 object structure :
Firestore-root
|
--- collection (collection)
|
--- document1 (document)
| |
| --- documentField (List<Object>): "Data1"
| : "Data2"
| : "Data3"
What I can do :
View_I_Can_Do
What I want to do :
View_I_Want_To_Have
NB : The number of ''Data'' is changing, so I can't design an itemView to match with an undefined number of variables.
I suppose the solution is in the onBindViewHolder method, but I don't know how to make it works.
Don't hesitate to ask for more details if needed.
Any help or suggestion would be appreciated, thanks in advance !
Edit 01/21/2022 :
I'm still stuck on this ...
I tried to give more detail in this question : Display one item for each object in a single Firebase document field - FirebaseRecyclerView
Maybe this is more explicit.

Firestore Java Android Create a structure with collections and nested documents

I am creating a post system on an android application I am using Firebase. The posts I have decided that I want to put them on the firestore because it is made to contain large amounts of data.
So, I want to use this data structure:
--- posts (collection)
|
--- uid (documents)
|
|
---- postId (documents)
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
[...] etc
I currently know how to add only one collection with the corresponding data schema, like so:
Map<String, Object> post = new HashMap<>();
post.put("title", title);
post.put("description", description);
post.put("valutation", valutation);
post.put("genre", filmGenre);
post.put("urlImage", urlImage);
post.put("date", dateSeq.toString());
dbRef.collection("posts").add(post);
But this would look like this on the firestore:
--- posts (collection)
|
|
--- title: "Post Title"
|
--- date: September 03, 2018 at 6:16:58 PM UTC+3
|
--- valutation: 8
[...] etc
So I would like to create with java the structure that I showed at the beginning of the post, could someone help me?
Collection or sub collection can have document.A document can't hold another document directly.so use below method to place document under "postId" sub collection.
Map<String, Object> post = new HashMap<>();
post.put("title", title);
post.put("description", description);
post.put("valutation", valutation);
post.put("genre", filmGenre);
post.put("urlImage", urlImage);
post.put("date", dateSeq.toString());
//you only need to pass uid and postId
dbRef.collection("posts").document(uid)
.collection(postId).add(post);

How do i understand firebase query in android? [duplicate]

I have a list of numbers inside that have certain data, in which I am adding a value "Sí" or "No", as in this image:
The last "Sí" that I added was the one that is in number 4, but if I do a filter in Firebase with
.equalTo("Sí").limitToLast(1)
I return the value of "Sí" positioned in the number 5 and not in the 4 that was the last "Sí" that I added to the database. There is some way to recognize the last "Sí", without the need of that is in the last position of the list?
I still can not find the solution to this, I hope you can help me.
Thank you.
There is some way to recognize the last "Sí", without the need of that is in the last position of the list?
Yes there is. The most common approach would be to add under each object a new property of type Timestamp named lastUpdate and then query descending according to it. Everytime you update a value, change the value also to lastUpdate with the current timestamp. This is how your schema might look like:
Firebase-root
|
--- Users
|
--- 1
| |
| --- Activo: "Si"
| |
| --- lastUpdate: 1561202277
|
--- 2
|
--- Activo: "No"
|
--- lastUpdate: 1561202299
This is how to save the timestamp:
How to save the current date/time when I add new value to Firebase Realtime Database
And this is how to order descending:
Firebase Data Desc Sorting in Android
How to arrange firebase database data in ascending or descending order?

How can i rename a branch/node in firebase

I have attached a picture, wherein i wish to rename the node during the run time for an application.
Short Answer:
Currently, Firebase doesn't allow you to rename a node/branch. So you might delete that node and create a new node again.
There is no api within Firebase for doing that. What can you do instead is to copy the information to another node and then simply delete the old one.
This is not a good practice to have the name of the product as a Firebase key. You need instead of using the product name, to use a unique identifier, an id. The best option is to use the unique key generated by the push() method. The name of the product will be then a child of your productId. Your database should look like this:
Firebase-root
|
--- prducts_details
|
--- -Ki-k6fM5GTRpQhGBRFRa
| |
| --- productName: "product1"
|
--- -Ki-oAAtTG1bWzLvKD5L
|
--- productName: "product2"
You have an option to Export the data. Once exported open and edit the exported.json. Here you can edit the node you want then save it and have Import to firebase db.

Categories