1

so I want to make a delete reply system but I don't know how to do it. Can you guy help me? This is my table image of my mongodb

and the code I've tried is but it doesn't work

app.post("/remove-reply", async (req, res) => {
const index = parseInt(req.body.index) - 1

await Posts.findOneAndUpdate({ _id: req.body.id }, [
    {
        $set: {
            "Comments.$[].replies": {
                $concatArrays: [
                    { $slice: ["$Comments.$[].replies", index] },
                    { $slice: ["$Comments.$[].replies", { $add: [1, index] }, { $size: "$Comments.$[].replies" }] }
                ]
            }
        }
    }
])
res.redirect("/")
})

and it gives me this error

    (node:17376) UnhandledPromiseRejectionWarning: MongoServerError: Invalid $set :: caused by :: FieldPath field names may not start with '$'.
    at MessageStream.messageHandler (C:\Users\The.Peerapon\Desktop\programming\Express-app\node_modules\mongodb\lib\cmap\connection.js:467:30)
    at MessageStream.emit (events.js:400:28)
    at processIncomingData (C:\Users\The.Peerapon\Desktop\programming\Express-app\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
    at MessageStream._write (C:\Users\The.Peerapon\Desktop\programming\Express-app\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
    at writeOrBuffer (internal/streams/writable.js:358:12)
    at MessageStream.Writable.write (internal/streams/writable.js:303:10)
    at TLSSocket.ondata (internal/streams/readable.js:731:22)
    at TLSSocket.emit (events.js:400:28)
    at addChunk (internal/streams/readable.js:293:12)
    at readableAddChunk (internal/streams/readable.js:267:9)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17376) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17376) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

my goal is to click delete button at reply to delete it by index.

5
  • idownvotedbecau.se/imageofcode Commented Nov 9, 2021 at 5:13
  • edited please help Commented Nov 9, 2021 at 5:17
  • This is better now. It would be even better if you can included a sample dataset and the expected output :) Mongoplayground is one of the convenient tool for sharing mongodb reproducible examples. Commented Nov 9, 2021 at 5:18
  • my expected output is to delete reply in the comment that is in the post by an index Commented Nov 9, 2021 at 5:25
  • I think we still need some clarifications as Comments is also an array. Do you mean you will be given 2 indexs, one for Comments and one for replies, so that you can remove the elements in replies by Comments.x.replies.y? As I said, a concrete expected output would be helpful for community here to understand the requirements. Commented Nov 9, 2021 at 5:56

1 Answer 1

1

for each comment\reply, save its index in the database and use it as a sort of id. then, when someone wants to delete a comment, you do the following:

app
  .post('/remove-reply', async (req, res) => {
    const postId = req.body.id;
    const commentIndex = parseInt(req.body.commentIndex) - 1;
    const replyId = req.body.replyIndex;
    await Posts.findOneAndUpdate({
      _id: postId
    }, {
      $pull: {
        [`Comments.${ commentIndex }.replies`]: {
          _id: replyId 
        }
      }
    });
    res.redirect('/');

  });

so, when a user clicks on a reply to delete, you should have all 3 fields you need for finding the exact reply you want to delete.

You can also just save an auto-generated unique id for each item to use as an identifier\index since we treat it as a collection more than an array in this use-case. It will probably be easier to maintain since after deleting a comment, the index is not aligned anymore with the underlying array.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! This worked correctly but your code is not correct at first but I have fixed your code to work
@UltimateSheepConfusedAtCode if you want, you can edit my answer on the site and change it to what worked for you. This will help people looking at this question+answer in the future to understand what works for this problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.