I have a subcollection of comments for the posts in a comments collection. However, how do I create a field (please, refer to the image) automatically?
I can create field manually from firestore console. But, I am not sure how to call it in the code. I cannot find it online. Without the field my subcollection document says:
This document does not exist, it will not appear in queries or snapshots
I use commentsRef().doc(widget.post.postId).collection('comments).set({data}) to create a comment, but how would I create a 'blahblah' field for the doc in sub-collection?
Thanks!
Your code is fine but you are trying to add field inside a collection which is not possible but just update your code to the following and it should work
commentsRef().doc(widget.post.postId).set({
'field': 'blahblah'
}).then((value) {
print('Document Updated');
});
Here is your result
Related
I'm working with a document and I want to create an empty collection inside the document.
FirebaseFirestore.instance.collection('Users').doc("thisdoc). ??? (create collection inside this document)
I don't know what to add instead of ???
The documentation will help you there.
NOTE The Firebase team is migrating these docs onto firebase.com directly, so this link may get old quickly in the next months.
EDIT. Since you need to create it under a subcollection, simply:
Take a reference of that document
Reference a subcollection in that document, even if it doesn't exist, yet
Create a first document in there, and your subcollection is made.
Pseudocode:
// WARN, PSEUDOCODE AHEAD
final documentRef = firestore.collection("myCol").document("myDoc");
final subCollectRef = documentRef.collection("mySubCol");
subCollectRef.add({
'your': 'data',
});
Apparently, I was able to retrieve a subcollection data from firebase by using the code below
FirebaseFirestore.instance.collectionGroup('announcementlist').where('id', isEqualTo: '${docID}');
However, I am now trying to delete a subcollection by using this collectionGroup method and it is not working. I want all admin user to be able to delete the subcollections.
So what I found online is something similar to this line of code.
// define document location (Collection Name > Document Name > Collection Name >)
var docRef = Firebase.firestore().collection("Rooms").doc("bsYNIwEkjP237Ela6fUp").collection("Messages");
// delete the document
docRef.doc("lKjNIwEkjP537Ela6fhJ").delete();
which I got from codegrepper but as far as I know, the doc("bsYNIwEkjP237Ela6fUp") part requires the id of the user which is not the method I want because all admin users should be able to delete the data and it uses collection instead of collectiongroup
Is there any solution for this ? Please help.
FirebaseFirestore.instance.collection("Rooms").doc(snapshot.requireData.docs[i].id).collection("Messages").doc(snapshot.requireData.docs[i].id).delete();
I want to update firestore document Id Check This
First, you cannot update the id of a document in Firestore. You will have to copy your document with the new id and delete the old one.
But why do you want to do this, especially within the Users collection? You will lose the link between the Firebase Auth Users and the Users details in your Firestore database.
You can't update the document id directly. For that you first need to make a new document with your updated Id and then delete the previous one.
First of all you need to get the document and store the snapshot value in the variable document.
DocumentSnapshot snapshot= await Firestore.instance.collection('Users').document('yourId').get();
var document=snapshot.data;
After that you can
Firestore.instance.collection('Users').document('newId').setData(document);
Firestore.instance.collection('Users').document(document.id).delete();
I'm using the below to code to delete the doc, but the delete operation is not done in cloudfirestore databased,still the data exist after delete, Please guide.
Collection id:users,
document id: p0lncbJnslgsvtUHW7zcbauk62F3
My cloud firestore version: cloud_firestore: ^0.13.4+2
await Firestore.instance
.collection(collectionId)
.document(documentId)
.delete();
When you see a document ID in italics in the console, that document doesn't actually exist. It's shown because that document ID has subcollections nested under it (which you are not showing in your screenshot. If you want to delete those subcollections, you will need to query for and delete each document in those subcollections. There is no easy way around this in client applications. For server-side code, there are tools to delete a document and subcollections recursively.
See also:
Firestore documentation: Delete collections
Firestore DB - documents shown in italics
Parent document is being marked as "deleted" (italics) by default
Why are non auto-generated document Ids are in italics in Firestore console?
I'm trying to delete a specific document in a collection. each document in that collection is named with an auto generated ID.
To achieve this, i need to get a reference to that document using its ID.
I'm using the cloud_firestore plugin for flutter. theres no such luck for a function in the docs
I'm hoping for something like this
var reference = Firestore.instance.refFromID(docID);
reference.delete();
oh hey, its pretty simple actually.
Firestore.instance.collection('c').document(docID).delete();
actually does the trick. passing the document ID into the document method gives you a reference to that document. you can do whatever you want with it afterwards