Home > database >  MongoDB: How do I delete nested object by index?
MongoDB: How do I delete nested object by index?

Time:11-09

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.

CodePudding user response:

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.

  • Related