Adding documents with same ID to Firestore at the same time

125 views
Skip to first unread message

Alvaro Gonzalez Rico

unread,
May 25, 2021, 8:16:06 AM5/25/21
to Flutter Development (flutter-dev)
Hello! I want to add 2 documents with the same ID to Firestore when doing the following: 
I want the bold one to have the same document ID as the non-bold one, since both are adding the same info to different places in Firestore. How could I do this? Thanks

void _sendPost() async {
 FirebaseFirestore.instance.collection('post').doc(docX).collection('posts').add({})
 FirebaseFirestore.instance.collection('users').doc(user.uid).collection('userPosts').add({})
}

Suzuki Tomohiro

unread,
May 25, 2021, 8:36:51 AM5/25/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
What’s the problem in the code?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/512c9c44-f35d-43a0-956e-7e526bb9fc6en%40googlegroups.com.

Alvaro Gonzalez Rico

unread,
May 25, 2021, 8:42:00 AM5/25/21
to Flutter Development (flutter-dev)
Oh there is no problem in the code. It is just that right now, the bold and non-bold give me different document IDs. I want to see if it is possible for them to have the same ID. Preferably if the bold can have the same doc ID as the non-bold.

Suzuki Tomohiro

unread,
May 25, 2021, 10:10:13 AM5/25/21
to Alvaro Gonzalez Rico, Flutter Development (flutter-dev)
It seems that you want to specify document ID when creating a document. Use "set" rather than "add".

> Set the data of a document within a collection, explicitly specifying a document identifier.




Alvaro Gonzalez Rico

unread,
May 25, 2021, 12:31:34 PM5/25/21
to Flutter Development (flutter-dev)
Thanks! I was able to find the solution.
Reply all
Reply to author
Forward
0 new messages