1

Im trying to update a certain property in my MongoDb.

This is the structure of the document:

"_id" : ObjectId("57e2645e11c979157400046e"),
        "id" : 1651570992420,
        "creator" : "nameHere",
        "title" : "titleHere,
        "content" : "contenthere",
        "comments" : [
                {
                        "id" : 1651733114268
                        "comment" : "commenthere"
                        "user": "userHere"
                        "timestamp": timeStampHere
                },
                {
                        "id" : 1651733114268
                        "comment" : "commenthere"
                        "user": "userHere"
                        "timestamp": timeStampHere
                }
        ]

Now im trying to change the comment property of the comments array for a certain id in comments. I'm using the following method to update the comment property:

async deleteComment(commentId: number, threadId: number): Promise<void> {
    try {
      threadModel.updateOne(
        { id: threadId, "comments.id": commentId },
        { $set: { "comments.$.comment": "deleted" } }
      );
    } catch (e: any) {
      console.log(e);
    }
  }

What I want to do is to simulate that a comment has been deleted by changeing the comment propery to the string "deleted".

However, the document doesnt get updated and I don't get an error message and I'm not sure how one would go about to debug an update to MongoDB. I can add these comments to the mongodatabse using a similar strategy but updateing doesn's seem to work. Any suggestions where I might have gone wrong?

Im trying to do something similar to this post: here

Maltec
  • 29
  • 4
  • Does this answer your question? [MongoDB update data in nested field](https://stackoverflow.com/questions/19603542/mongodb-update-data-in-nested-field) – Liam May 05 '22 at 09:36
  • "nothing happens" ... the document is unchanged? Are there any error messages? – rickhg12hs May 05 '22 at 09:37
  • [you should replace the $ with the zero-based index](https://stackoverflow.com/a/19607157/542251) – Liam May 05 '22 at 09:37
  • @rickhg12hs Sorry for being unclear. I updated the post to: However, the document doesnt get updated and I don't get an error message. – Maltec May 05 '22 at 09:55
  • @Liam aaah, yes that solves it. That's what I get for skimming the mongoDB webpage... However I won't know on what index the comment is on. Do I need to use findOne() first or is there any other way of doing it when I don't know the index? – Maltec May 05 '22 at 09:56
  • @Liam After some trial and error I'm not sure that this solves it since I don't have an array of an array. My idea was that the second parameter for commentId should find my comment in the comments array. Since I won't know on what index the comment is on. Do I need to use findOne() first or is there any other way of doing it when I don't know the index? – Maltec May 05 '22 at 10:05
  • @Liam I'm trying something similar to [this.](https://stackoverflow.com/a/61919092/14292752) But mine doesn't work. Must be something stupid im forgetting but I can't seem to figure it out. – Maltec May 05 '22 at 10:30
  • 1
    If you use ```const result = await threadModel.updateOne( ... query/update stuff ...); console.log( `${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)` );```, what does the console log show? – rickhg12hs May 05 '22 at 10:31
  • @rickhg12hs I seem to be getting this error: MongooseError: Query was already executed: ..... and then alot of my files – Maltec May 05 '22 at 10:52
  • 1
    @rickhg12hs I solved it by adding an "await" that I forgot. Thank you both for helping me – Maltec May 05 '22 at 10:54

1 Answers1

1

I had forgotten to add an await. The code that worked looked like:

async deleteComment(commentId: number, threadId: number): Promise<void> {
    console.log(commentId + " " + threadId);
    try {
      const result = await threadModel.updateOne(
        { id: threadId, 'comments.id': commentId },
        { $set: { "comments.$.comment": 'deleted' } }
      );
    } catch (e: any) {
      console.log(e);
    }
  }
Maltec
  • 29
  • 4