This question already has answers here:
Firestore DB - documents shown in italics
(3 answers)
Closed 20 days ago.
I wanted to save Firebase data via the admin panel. I have everything set up and ready to save data. When I save the data, this data appears in italics in the Firestore and I cannot pull this data from my normal application. What should I do to solve this? Thanks.
I leave images that I can show as examples.
Here, I throw the data I get into a HashMap and send it to the function to save it in Firestore, it enters the Firestore, but this record is italicized and I cannot read this data. The problem is right here. (Example 1)
When I try to save the data manually on the Firestore, it records without any problems and I can read this data from the application. (Example 2, Example 3)
The document with is listed in italics ("Example 1") is not actually a document that exists in the collection. The italics style only means that under this document, there are other subcollections present. If you try to query such documents, you won't get a result, since such documents don't exist. So that document is displayed like that because of two main reasons. You either deleted the document without deleting all the documents in all of their subcollections, or you didn't create it in the first place. When you see that, it just means that you reserved a document ID for later use. Meaning that such documents will remain visible in the console because you might want to navigate into their subcollections.
One thing to remember, in Firestore documents and their corresponding sub-collections don't work like filesystem files and directories on the disk. If you create a sub-collection under a document, it doesn't implicitly create any parent documents. Sub-collections are not tied in any way to a parent document.
If you want to correct that, you have to write at least a property that can hold a value under the "Example 1" document. Even a null value will be sufficient. On the other hand, if you delete a document, its sub-collections will continue to exist.
Related
is there any way to get documents from firebase by list of IDs?
I have this list:
val IDs= listOf("id1","id2","id3","id4")
and I want to get all those documents without looping through them.
something like this, if possible:
Firebase.firestore.collection("users").documents(IDs).get()
The Firestore client libraries don't have the notion of batch reads which would allow to retrieve a set of documents given it's ids. Nonetheless, you can still make queries that filter on the document id by using the FieldPath.documentId() object. This value translates to a special sentinel that allows queries on the document id so you can write a query like below:
Firebase.firestore.collection("users")
.whereIn(FieldPath.documentId(), listOf("id1", "id2", [...], "id10"))
There's one restriction to note with this approach. The whereIn filter accepts at most 10 values. If you need to retrieve more it would be necessary to make several queries.
Alternatively, the approach suggested by Brettski of making a bunch of single document reads would work too.
That isn't possible in Firestore. Id's can only be accessed directly through a collection/document request.
I don't know Kotlin, though in JavaScript I would perhaps approach it by creating an array of functions for each of the documents, then using a Promise.all() to retrieve them at once. It is, imo a downside of Firestore where you can't query for multiple Id's.
Are db.document("a").collection("b") and some_var = db.document("a"); some_var_2 = some_var.collection("b") equivalent?
In particular: in both cases, are "a"'s fields retrieved from database and loaded in RAM (especially for the first way)? (in other words: does the first way just retrieve the subcollection without retrieving/loading "a"'s fields?)
In both cases, you will only generate document or collection references, so Firestore won't fetch any data until you call a method (such as .get()) on it.
So, there is no difference, but you should use the first way in the case you will need to make operations on the entire collection in addition to the single document.
They are equivalent.
Neither of them actually retrieve any data. They are just creating references to documents. You have to call get() or attach a listener to actually get the data from the documents. You should probably familiarize yourself with the documentation for reading data from Firestore.
I'd like to create an application that includes something like an rss feed or a feed that you might see instagram or facebook. I'm currently trying to accomplish this using the firebase real time database or firestore as a backend to store the posts shown to the users. I can't seem to think of a way to make this work. The ideal solution sends a list of posts in chronological order that doesn't need any additional sorting on the client. When I tried to use the real time database, I could easily add all the relevant posts in their own path and sort them on the client side (or maybe use cloud functions to sort the data on the server side). When trying to come up with the solution using firestore, my idea was to have the post document hold a reference to a sub collection that holds the list of subscribers/followers but I can't seem to find a way to select a post based on whether a document exists in a referenced sub collection.
FYI, the reason why I want the ideal solution is because PAGINATION.
I have little to no experience with Firestore. With Firebase however, if I understand you correctly, this is easy to achieve.
The first part is storing your date, make sure you store it as a timestamp (date.getTime()).
Then you can do the following:
db.ref('posts').orderByChild('date').limitToLast(10)
This allows you to fetch the last 10 posts. To get the previous 10 you grab the date from the earliest of those posts and do:
db.ref('posts').orderByChild('date').endAt(previousDate - 1).limitToLast(10)
Note that you might want to handle this differently if 2 posts have the same date value.
I have a database in Firestore that is available for all my users. This way updates made by me to this database is instant updated for my users.
My challenge is that my users can edit some of the data in the database - user preferred values. These changes can of course not be written to the public database, but must either be written to a local SQLite-database on the phone or somehow written to each users private collection of documents in firestore.
A SQLite solution means that each time a document from Firestore is displayed, I need to read the local SQLite database to check for changes.
Using private collection of documents in Firestore means that I have to read two documents for each item that I want to display.
I struggle to find the "perfect" solution for this situation.
What is the best approach? Is there a solution I haven't thought of?
I believe the best approach is Firestore Security Rules.
Any data for a user should go in a special document: /users/userId
Set up the firestore rules so that this document is only writable by the user with ID === userId. You can make the document readable by all if you need or limit it to only the user who created it.
All of the Firestore data retrieval examples show a full document getting returned. Examples like this:
// Create a reference to the cities collection
CollectionReference citiesRef = db.collection("cities");
// Create a query against the collection.
Query query = citiesRef.whereEqualTo("state", "CA");
My database has 3,000 city objects in it, and I need to get a list of all the unique states in my database. Can someone show me an example of how I can get this information from Firestore? I'm hoping I don't need to download all 3000 documents just to collate this list myself.
You could create a Cloud Function which is triggered by an onWrite event to your cities collection. This function could see if the state already exists in a states collection and, if not, add it.
This way, you'll end up with a states collection which only has one of each state in it. As states are generally a 2 letter code, you could use this code as the document index, to ensure uniqueness in your cloud function writes.
As Frank says, the only way in which you can achieve this is by duplicationg data. This tehnique is named denormalization and for that I'll share you another resourse, which is a tutorial that I personally recomend you to see for a better understanding, Denormalization is normal with the Firebase Database. This tutorial was made for Firebase Realtime database but the same principles are also in Cloud Firestore.
So to solve your problem, you need to create another top level collection in which you need to store only the unique states. But you need to be aware of the fact that every time you add or delete a document, you need to do it twice, once for the cities collection that you already have and second for the newly created collection uniqueStates.