I have a firestore documented modeled like this:
In each document, there can be multiple messages, and in each message, there can be multiple comments. Im trying to update the comments with the following:
Future<void> updateStingrayComment(
Comment? comment, String? stingrayid) async {
return FirebaseFirestore.instance
.collection('stingrays')
.doc(stingrayid)
.update({
'messages': {
'comments': FieldValue.arrayUnion([Comment.toJson(comment)])
}
})
I recognize Im not using a messageID to attach the comment to, but I’m not sure how I would do that. Anyone have any ideas? Im using this in flutter, if that makes any difference.
2
Answers
For anyone with a similar situation, I ended up with a kindof brute force solution. Instead of a nested list of comments, I made a separate list called 'comments'. Every message will have a message ID, which gets passed to respective comments, and all comments will be queried based on their message ID.
It looks like
messages
is an array field, and there is no operation to update an item in an array field.You will have to: