Excuse me,
I'm working on my code about making a Document Firestore that could be delete, this is how my Firestore Database looks like :
Firestore Data Structure
I'd like to delete the Auto-ID Document after the Collection "X-MIPA.6". So, I followed the previous solution about "Firebase Cloud Firestore get auto-id of newly created document", so here is my code looks like :
My Code
But, when I tested, my app force closed and show me this Error :
The Error
How is it possible? How can I fix it?
I want to delete the Document based on the Auto-ID Document on my Firestore Database.
Thanks!
Related
I made a simple chat app using Flutter. My app is functioning perfectly fine (getting and dumping data into firebase) but once my messages get into my database, they are randomly ordered resulting in my msgs getting displayed in a random order.
I have tried reversing the SnapshotQueue in my Flutter code but that did not help...
You will need to attach document names to your documents before uploading them.
I think you are experiencing this issue because your documents are being given Auto Ids.
Try using the current timestamp as the document name. this will help arrange documents in order according to time uploaded.
Firestore.instance.collection(CollectionName).document(Timestamp.now()).setData(messageMap);
i hope this is what you need. if not. Please share your code that uploads the message to database.
Add a field like datePublished = DateTime.now() to each message document as
it gets created to
store the timestamp of when the message was created
Then you can use datePublished field to order your documents of your QuerySnapshot like this:
QuerySnapshot snapshot = await collection.orderBy('datePublished', descending: true).get();
now your messages are arranged in chronological order from the latest
to earliest.
When I go to delete a document from the Firestore console, I get the following message:
"This will permanently delete all data at this location, including all nested data."
However, the Firestore documentation reads: "Deleting a document does not delete the documents in its subcollections"
Can someone clarify this relationship for me?
If I delete a document from the Firestore console, it appears to remove all subcollections and nested data. If it doesn't, how can I delete said sub collections since I can no longer see them?
The console will do exactly what it says - delete the document and all of the subcollections organized under it.
The client APIs will not. If you delete a document using the standard delete() method on a document reference, it will not delete anything organized under it.
So few day ago I moved my apps posts to cloud firestore from realtime database because of the more flexable querying that firestore apparently has. I have been modifying my code to work with firestore as the new database. Now I ran into a problem. How can I retrieve every document within "Posts" collection that has "Likes" collection that contains a specifically named document? I don't know if that makes sense so here's an image showcasing the problem:
I had a working query for firebase realtime database but can't figure out how to do it with firestore. Is it possible or do I have to change the structure of my database? If I do have to change the structure, what would be the most convenient way of getting the same result?
This is currently not possible with collection group queries. You might be tempted to put a filter on the query for FieldPath.documentId() == 'your-doucment-id', but that just isn't supported. FieldPath.documentId() is actually a token that refers to to fully unique document id, which includes the entire path of the document.
The workaround is to store the document ID as a field in the document, and filter on the contents of that field instead. So, if the document had a field called id with the ID of the document, you could query for collectionGroup("Likes").whereEquals('id', 'your-document-id').
I am currently trying the firebase realtime database and I am executing my first queries. I am using the Firebase Admin console for querying.
I now have a problem that I only get one entry back by a query when using ordering. My data structure looks like the following:
The query I am using is:
firebase().database().ref('/Office1/Department1/HR').orderByChild('Rank').on('value', function (snapshot) {
snapshot.forEach(function(childSnap){
console.log(childSnap.val());
});
})
As an output I only get:
{"Name":"Charles","Rank":1}
As you can see the ordering works, however it only shows one item instead of all 3 entries.
Somebody knows how to fix this issue?
Thanks
I'm using Firebase Cloud Firestore and can't figure out how to access documents without knowing the specific path. The database structure is users/{user id}/favourites/{favourite id}. There are no fields in users/{user id} only subcollections. Knowing the user id, i can get the favourites for the user, but I can't get a list of users to get everyone's favourites. Here is the code I am trying (Java admin SDK):
db.collection("users").get().get()
which results in an empty Iterable with no DocumentSnapshots.
How can I get a list of the most popular favourites?
EDIT: I've discovered I can get a list of users with no fields if I add a field. Even if I delete it later, it still appears as a document in the collection.
EDIT2: I've discovered that I can create an empty document, so I'm just doing that for now. As a one-off, I can get a list of all users from firebase auth and look up which ones have a favourites collection and just set those to empty documents.