skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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:

    1. Read the array from the document into your application code
    2. Update the array in memory
    3. Write the entire array back to the document
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search