0

I'm new to MongoDB using angular as frontend. I'm trying to update a name in nested object array.

My Schema is as follows:

const mongoose = require("mongoose");

const projectDragDropSchema = mongoose.Schema({
  _idProject: mongoose.Schema.Types.ObjectId,
  projectTitle: { type: String, required: true },
  boards: [
    {
      _idBoard: mongoose.Schema.Types.ObjectId,
      boardTitle: { type: String, required: false },
      cards: [
        {
          type: new mongoose.Schema(
            {
              cardId: { type: mongoose.Schema.Types.ObjectId, required: true },
              cardTitle: { type: String, required: false },
            }
            // { minimize: false }
          ),
          required: false,
        },
      ],
      required: false,
    },
  ],
});

module.exports = mongoose.model("ProjectDragDrop", projectDragDropSchema);

I'm trying to update the cardTitle. I have written the multiple updates to it, but unable to find the correct one.

The Router:

router.patch(
  "/updateProjectBoardCardName/:_idProject/:_id",
  projectBoardsCards.updateCardName
);

The code:

exports.updateCardName = (req, res) => {
  const idProject = req.params._idProject;
  const boardID = req.params._id;
  projectDragDropSchema
    .update(
      { _idProject: idProject, "boards._id": boardID },
      { cards: { $elemMatch: { _id: req.body.params } } },
      { $set: { "cards.$.cardTitle": req.body.params } }
    )
    .exec()
    .then((result) => {
      console.log(result);
      res.status(200).json(result);
    })
    .catch((err) => {
      console.log(err);
      res.status(500).json({
        error: err,
      });
    });
};

Thanks in advance.

nihar
  • 23
  • 1
  • 5
  • `{ cards: { $elemMatch: { _id: req.body.params } } }` <-- is the value of `req.body.params` the id? If so why are you setting the id as a cardTitle --> `{ $set: { "cards.$.cardTitle": req.body.params } }` What is the value of `req.body.params`? – Molda Jul 15 '20 at 18:12
  • Also you are setting the type of cards as `cards: [{ type: new mongoose.Schema(` I can't see in the docs schema being a valid type. It should be more like `cards: [new mongoose.Schema(...)]` but i think you should define it like this `var cardsSchema = new mongoose.Schema(...)` then use it like this `cards: [cardsSchema]` – Molda Jul 15 '20 at 18:22
  • @Molda Thank you. The initial problem that I am facing is that the schema is wrong(I know it, but I had the similar schema which was working fine). The other problem is that, I am getting only _idProject, but not the other ids (the _idCard,_idBoard) but getting the only _id for both cards and boards. Can you help me out here? – nihar Jul 16 '20 at 06:45
  • Can you show me the actual data from the db? Using `projectDragDropSchema.findOne()` should return the first item from DB, add it to your question. – Molda Jul 16 '20 at 07:29
  • @Molda Thank you. I guess the whole thing which I'm doing is wrong, as in the nested arrays. It should like like you said. So, right now, I'm doing is creating a separate schema for the cards array and will be referencing it the main schema and then try to update. Because,according to this link; [https://stackoverflow.com/questions/23577123/updating-a-nested-array-with-mongodb] it is becoming quite complex. So I'll try it out by creating a new schema and referencing it to my main schema – nihar Jul 17 '20 at 08:17
  • @Molda Is there anyway I can contact you this weekend? It would be really helpful if you could explain me the schemas and and referencing schemas. I really need help in understanding this. (I started learning nodejs last monday!) – nihar Jul 17 '20 at 15:49
  • Funny enough, you are the third one this week to ask me for a chat. I don't have time for that i'm sorry. I'm pretty sure there's enough tutorials to read/watch to help you to understand it. It just takes some time. – Molda Jul 17 '20 at 18:32

0 Answers0