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);
Related
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()?
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.
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?
I don't know how I can achieve this data structure in Firestore.
I have this structure:
Firestore-root
|
--- liturgia (collection)
| |
| --- lh (documents)
| |
| --- oficio (collection)
| |
| --- 03070101 (documents)
| |
| --- himno: "Today himno for today oficio"
| |
| --- read: "Today read for today oficio"
|
| --- 03070102 (documents)
| |
| --- himno: "Today himno for today oficio"
| |
| --- read: "Today read for today oficio"
|
| --- 09070325 (documents)
| |
| --- himno: "Today himno for today oficio"
| |
| --- read: "Today read for today oficio"
|
| --- laudes (collection)
| |
| --- 03070101 (documents)
| |
| --- himno: "Today himno for today laudes"
| |
| --- read: "Today read for today laudes"
|
| --- 03070102 (documents)
| |
| --- himno: "Today himno for today laudes"
| |
| --- read: "Today read for today laudes"
|
| --- 09070325 (documents)
| |
| --- himno: "Today himno for today laudes"
| |
| --- read: "Today read for today laudes"
In oficio or in laudes there are documents with fixed content, this content never change, and I need to use that content according to the date.
For example:
I have any date, on this date, one entire content is needed. For example, when I need to show the today content for oficio (or for laudes):
For 2019-03-25 i will need the content of 09070325
For 2019-03-26 i will need the content of 03070102
...
For 2020-03-25 i will need the content of 09070325
For 2020-03-26 i will need the content of 03070103
...
For 2021-03-25 i will need the content of 03070101
I think of a structure to relate the fixed content according to the dates, but I do not know how to do it.
In my application, the current date will be taken, and based on this data, must find the fixed content for oficio, for laudes, etc.
For example, in 2019-03-25 and in 2020-03-25 is needed the content 09070325, but in 2021-03-25 is needed the content 03070101:
20190325
oficio : 09070325
laudes : 09070325
20190326
oficio : 03070102
laudes : 03070102
20200325
oficio : 03070101
laudes : 03070101
20210325
oficio : 03070101
laudes : 03070101
How can I organize my data structure to:
not having to update the fixed content documents
put in relation the fixed content with the dates each year
obtain the fixed content according to a given date consuming the least amount of resources
The simplest solution I can think of is to use a date property of type String for each of your documents and simplify your structure a little bit. So a possible structure might be:
Firestore-root
|
--- liturgia (collection)
|
--- 03070101 (document)
|
--- himno: "Today himno for today oficio"
|
--- read: "Today read for today oficio"
|
--- date: "20190325"
|
--- length: "1h"
To get today's document, simply use the following query:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference liturgiaRef = rootRef.collection("liturgia");
Query query = liturgiaRef.whereEqualTo("date", today);
In which today is 20190325. That's it!
Edit:
According to your comment, instead of storing the data as date: "20190325" store it only as date: "0325". This means that you can query according to the month and day no matter what the year is. In this case, today is only 0325. This way it will work for evey year.
Edit2:
According to OP's comments, we agree that a possible solution would be to create a calendar node that relates the dates and the contents.
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.